Skip to content

Commit

Permalink
zit command to use app.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakovlenko committed Oct 29, 2024
1 parent 718dcd5 commit eafff65
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
18 changes: 17 additions & 1 deletion cmd/zit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@ package main
import (
"fmt"
"os"
"zit/internal/app"
"zit/internal/config"
"zit/internal/doctor"
"zit/internal/identity"
"zit/internal/version"

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

const AppVersion = "v3.1.0-alpha.2"

func main() {

userHomeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

appConfig := app.Config{
FS: afero.NewOsFs(),
UserHomeDir: userHomeDir,
ConfigPathFromEnv: os.Getenv(config.EnvVarName),
XDGHomePathFromEnv: os.Getenv(config.XdgEnvVarName),
}

app := &cli.App{
Name: "zit",
Usage: "git identity manager",
Commands: []*cli.Command{
version.VersionCmd(AppVersion),
doctor.DoctorCmd,
identity.SetCmd,
config.ConfigCmd,
config.ConfigCmd(appConfig),
},
}

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

import "github.com/spf13/afero"

type Config struct {
FS afero.Fs
UserHomeDir string
ConfigPathFromEnv string
XDGHomePathFromEnv string
}
119 changes: 57 additions & 62 deletions internal/config/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,69 @@ package config
import (
"encoding/json"
"fmt"
"os"
"zit/internal/app"

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

var ConfigCmd = &cli.Command{
Name: "config",
Subcommands: []*cli.Command{
configPathCmd,
configShowCmd,
},
func ConfigCmd(appConfig app.Config) *cli.Command {
return &cli.Command{
Name: "config",
Subcommands: []*cli.Command{
configPathCmd(appConfig),
configShowCmd(appConfig),
},
}
}

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
},
func configPathCmd(appConfig app.Config) *cli.Command {
return &cli.Command{
Name: "path",
Action: func(_ *cli.Context) error {
confPath, err := LocateConfFile(
appConfig.FS,
appConfig.UserHomeDir,
appConfig.ConfigPathFromEnv,
appConfig.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
},
func configShowCmd(appConfig app.Config) *cli.Command {
return &cli.Command{
Name: "show",
Action: func(_ *cli.Context) error {
confPath, err := LocateConfFile(
appConfig.FS,
appConfig.UserHomeDir,
appConfig.ConfigPathFromEnv,
appConfig.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
},
}
}

0 comments on commit eafff65

Please sign in to comment.