Skip to content

Commit 4529c7e

Browse files
committed
add list/reload/show commands
1 parent 34bfd12 commit 4529c7e

File tree

10 files changed

+225
-73
lines changed

10 files changed

+225
-73
lines changed

commands/common.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
const (
11+
CONFIG_1 = ".envok.yaml"
12+
CONFIG_2 = ".envok.yml"
13+
)
14+
15+
func getCurrentProfile() string {
16+
const currentProfileNameFilePath = ".config/envok/prof_name.txt"
17+
18+
home, err := os.UserHomeDir()
19+
filePath := fmt.Sprintf("%s/%s", home, currentProfileNameFilePath)
20+
data, err := os.ReadFile(filePath)
21+
var currentProfileName string
22+
23+
if err != nil {
24+
fmt.Printf("%s\n", err)
25+
currentProfileName = ""
26+
} else {
27+
currentProfileName = strings.TrimSpace(string(data))
28+
}
29+
30+
return currentProfileName
31+
}
32+
33+
func getConfigPath() (string, error) {
34+
35+
fileName, err := searchFileUpwards(CONFIG_1)
36+
if err != nil {
37+
fileName, err = searchFileUpwards(CONFIG_2)
38+
if err != nil {
39+
return "", fmt.Errorf("failed to get config file %s (or %s)\n", CONFIG_1, CONFIG_2)
40+
}
41+
}
42+
43+
return fileName, nil
44+
45+
}
46+
47+
func searchFileUpwards(filename string) (string, error) {
48+
currentDir, err := os.Getwd()
49+
if err != nil {
50+
return "", fmt.Errorf("failed to get current directory: %w", err)
51+
}
52+
53+
homeDir, err := os.UserHomeDir()
54+
if err != nil {
55+
return "", fmt.Errorf("failed to get home directory: %w", err)
56+
}
57+
58+
for {
59+
filePath := filepath.Join(currentDir, filename)
60+
61+
if _, err := os.Stat(filePath); err == nil {
62+
return filePath, nil // File found
63+
}
64+
if currentDir == homeDir || currentDir == "/" {
65+
break
66+
}
67+
68+
// Move up one directory
69+
currentDir = filepath.Dir(currentDir)
70+
}
71+
72+
return "", fmt.Errorf("file %q not found", filename)
73+
}

commands/export.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/ciur/envok/profiles"
8+
)
9+
10+
func ExportProfile(name string) {
11+
configPath, err := getConfigPath()
12+
if err != nil {
13+
fmt.Printf("Error getting config: %s", err)
14+
os.Exit(1)
15+
}
16+
17+
items, err := profiles.Load(configPath)
18+
if err != nil {
19+
fmt.Printf("Error loading profiles: %s\n", err)
20+
os.Exit(1)
21+
}
22+
23+
for _, profile := range items {
24+
if profile.Name == name {
25+
for k, v := range profile.Vars {
26+
fmt.Printf("export %s=%s\n", k, v)
27+
}
28+
}
29+
}
30+
}

commands/list.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ import (
88
)
99

1010
func ListProfiles() {
11-
items, err := profiles.Load(".envok.yaml")
11+
12+
currentProfileName := getCurrentProfile()
13+
configPath, err := getConfigPath()
14+
if err != nil {
15+
fmt.Printf("Error getting config: %s", err)
16+
os.Exit(1)
17+
}
18+
19+
items, err := profiles.Load(configPath)
1220
if err != nil {
1321
fmt.Printf("Error loading profiles: %s\n", err)
1422
os.Exit(1)
1523
}
1624
for _, profile := range items {
17-
fmt.Printf("%s\n", profile.Name)
25+
if profile.Name == currentProfileName {
26+
fmt.Printf("%s*\n", profile.Name)
27+
} else {
28+
fmt.Printf("%s\n", profile.Name)
29+
}
1830
}
1931
}

commands/reload.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package commands
2+
3+
func ReloadCurrentProfile() {
4+
name := getCurrentProfile()
5+
ExportProfile(name)
6+
}

commands/show.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/ciur/envok/profiles"
8+
"github.com/fatih/color"
9+
)
10+
11+
func ShowCurrentProfile() {
12+
13+
currentProfileName := getCurrentProfile()
14+
configPath, err := getConfigPath()
15+
if err != nil {
16+
fmt.Printf("Error getting config: %s", err)
17+
os.Exit(1)
18+
}
19+
20+
items, err := profiles.Load(configPath)
21+
if err != nil {
22+
fmt.Printf("Error loading profiles: %s\n", err)
23+
os.Exit(1)
24+
}
25+
26+
if currentProfileName == "" {
27+
fmt.Println("No current profile set")
28+
return
29+
}
30+
31+
fmt.Printf("---%s---\n", currentProfileName)
32+
33+
for _, profile := range items {
34+
if profile.Name == currentProfileName {
35+
for k, v := range profile.Vars {
36+
envVarValue := os.Getenv(k)
37+
if envVarValue != v {
38+
color.Red("%s=<value differ: expected=%q, actual=%q>\n", k, v, envVarValue)
39+
} else {
40+
color.Green("%s=%s\n", k, v)
41+
}
42+
}
43+
}
44+
}
45+
}

config/currentProfile.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

envok.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,55 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
6+
"os"
57

68
"github.com/ciur/envok/commands"
79
)
810

11+
const VERSION = "0.2"
12+
13+
var profileName = flag.String("p", "", "Profile name")
14+
var version = flag.Bool("v", false, "show version and exit")
15+
916
func main() {
1017
flag.Parse()
18+
19+
flag.Usage = func() {
20+
w := flag.CommandLine.Output()
21+
22+
fmt.Fprintf(
23+
os.Stderr,
24+
"Usage: %s [-p profile-name] export|list|show|reload\n",
25+
os.Args[0],
26+
)
27+
28+
flag.PrintDefaults()
29+
30+
fmt.Fprintf(w, "For more details check: https://github.com/ciur/envok\n")
31+
}
32+
33+
if *version {
34+
fmt.Println(VERSION)
35+
os.Exit(0)
36+
}
37+
1138
args := flag.Args()
1239
if len(args) == 0 {
40+
flag.Usage()
1341
return
1442
}
1543

16-
if args[0] == "list" {
44+
switch args[0] {
45+
case "list":
1746
commands.ListProfiles()
47+
case "export":
48+
commands.ExportProfile(*profileName)
49+
case "show":
50+
commands.ShowCurrentProfile()
51+
case "reload":
52+
commands.ReloadCurrentProfile()
53+
default:
54+
flag.Usage()
1855
}
19-
2056
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ module github.com/ciur/envok
22

33
go 1.24.1
44

5-
require gopkg.in/yaml.v3 v3.0.1 // indirect
5+
require (
6+
github.com/fatih/color v1.18.0 // indirect
7+
github.com/mattn/go-colorable v0.1.13 // indirect
8+
github.com/mattn/go-isatty v0.0.20 // indirect
9+
golang.org/x/sys v0.25.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
2+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
11+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
112
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
213
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
314
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

profiles/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func convertToStringMap(data interface{}) (Vars, error) {
4747
func Load(fileName string) ([]Profile, error) {
4848
var profiles []Profile
4949

50-
config, err := loadConfig(".envok.yaml")
50+
config, err := loadConfig(fileName)
5151
if err != nil {
5252
log.Fatalf("Error loading config: %v", err)
5353
}

0 commit comments

Comments
 (0)