Skip to content

Commit

Permalink
feat(cli): add config command
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakovlenko committed Oct 29, 2024
1 parent 8cd497e commit 718dcd5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/zit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"zit/internal/config"
"zit/internal/doctor"
"zit/internal/identity"
"zit/internal/version"
Expand All @@ -20,6 +21,7 @@ func main() {
version.VersionCmd(AppVersion),
doctor.DoctorCmd,
identity.SetCmd,
config.ConfigCmd,
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (err *ErrConfigNotFound) Error() string {
if err.EnvVar {
envVar = " defined in " + EnvVarName + " variable"
}

return fmt.Sprintf("config file%s is not found at %s", envVar, err.Path)
}

Expand Down
76 changes: 76 additions & 0 deletions internal/config/config_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package config

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/afero"
"github.com/urfave/cli/v2"
)

var ConfigCmd = &cli.Command{
Name: "config",
Subcommands: []*cli.Command{
configPathCmd,
configShowCmd,
},
}

var configPathCmd = &cli.Command{
Name: "path",
Action: func(_ *cli.Context) error {
fs := afero.NewOsFs()

userHomeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot user home dir: %v", err)
}

configPathFromEnv := os.Getenv(EnvVarName)
xdgHomePathFromEnv := os.Getenv(XdgEnvVarName)

confPath, err := LocateConfFile(fs, userHomeDir, configPathFromEnv, xdgHomePathFromEnv)
if err != nil {
return fmt.Errorf("could not locate config file: %w", err)
}

fmt.Println(confPath)

return nil
},
}

var configShowCmd = &cli.Command{
Name: "show",
Action: func(_ *cli.Context) error {
fs := afero.NewOsFs()

userHomeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot user home dir: %v", err)
}

configPathFromEnv := os.Getenv(EnvVarName)
xdgHomePathFromEnv := os.Getenv(XdgEnvVarName)

confPath, err := LocateConfFile(fs, userHomeDir, configPathFromEnv, xdgHomePathFromEnv)
if err != nil {
return fmt.Errorf("could not locate config file: %w", err)
}

conf, err := Load(confPath)
if err != nil {
return err
}

confJson, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return err
}

fmt.Println(string(confJson))

return nil
},
}
7 changes: 7 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Load(filename string) (*ConfigRoot, error) {
if err != nil {
return nil, err
}

return parseYaml(contents)
default:
return nil, fmt.Errorf("something went horribly wrong")
Expand All @@ -40,6 +41,7 @@ func formatFromFilename(filename string) string {
if strings.HasSuffix(filename, ".yaml") {
return yamlFormat
}

return otherFormat
}

Expand All @@ -50,6 +52,7 @@ func LocateConfFile(fs afero.Fs, userHomeDir, confPathFromEnv string, xdgHomeFro
if fileExists(fs, confPathFromEnv) {
return confPathFromEnv, nil
}

return "", &ErrConfigNotFound{
EnvVar: true,
Path: "'" + confPathFromEnv + "'",
Expand All @@ -62,6 +65,7 @@ func LocateConfFile(fs afero.Fs, userHomeDir, confPathFromEnv string, xdgHomeFro
if xdgHomeFromEnv == "" {
xdgHomeFromEnv = path.Join(userHomeDir, ".config")
}

xdgYamlDefault := path.Join(xdgHomeFromEnv, "zit", "config.yaml")
if fileExists(fs, xdgYamlDefault) {
return xdgYamlDefault, nil
Expand All @@ -86,9 +90,11 @@ func fileExists(fs afero.Fs, filename string) bool {
if os.IsNotExist(err) {
return false
}

if fileInfo.IsDir() {
return false
}

return true
}

Expand All @@ -97,5 +103,6 @@ func parseYaml(contents []byte) (*ConfigRoot, error) {
if err := yaml.Unmarshal(contents, &config); err != nil {
return nil, err
}

return &config, nil
}

0 comments on commit 718dcd5

Please sign in to comment.