From d8a38b1339c6cc4db1139ac032b211ceb6d04c22 Mon Sep 17 00:00:00 2001 From: anoriqq Date: Tue, 19 Jul 2022 01:03:57 +0900 Subject: [PATCH] feat: add aquifer-dir arg to install cmd --- internal/cmd/install.go | 14 +++++++++++++- internal/config/config.go | 23 +++++++++++++++++++---- internal/service/aquifer/aquifer.go | 4 ++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/internal/cmd/install.go b/internal/cmd/install.go index 1b1add3..dd81132 100644 --- a/internal/cmd/install.go +++ b/internal/cmd/install.go @@ -3,6 +3,7 @@ package cmd import ( "errors" + "github.com/anoriqq/qpm/internal/config" "github.com/anoriqq/qpm/internal/service/aquifer" "github.com/spf13/cobra" ) @@ -13,8 +14,11 @@ var installCmd = &cobra.Command{ RunE: installRun, } +var aquiferDir string + func init() { rootCmd.AddCommand(installCmd) + installCmd.PersistentFlags().StringVarP(&aquiferDir, "aquifer-dir", "d", "", "aquifer dir") } func installRun(_ *cobra.Command, args []string) error { @@ -23,7 +27,7 @@ func installRun(_ *cobra.Command, args []string) error { return err } - return aquifer.Install(pkgName) + return aquifer.Install(getAquiferDir(), pkgName) } func getPkgName(args []string) (string, error) { @@ -33,3 +37,11 @@ func getPkgName(args []string) (string, error) { return args[0], nil } + +func getAquiferDir() string { + if len(aquiferDir) != 0 { + return aquiferDir + } + + return config.Cfg.AquiferDir +} diff --git a/internal/config/config.go b/internal/config/config.go index 06d5e8e..df04b2b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -83,12 +83,21 @@ func Load() error { return nil } +const ( + ConfigNameAquiferDir = "AquiferDir" + ConfigNameAquiferRepoURL = "AquiferRepoURL" + ConfigNameGithubUsername = "GitHubUsername" + ConfigNameGithubAccessToken = "GitHubAccessToken" +) + +// aquifer dir + func hasAquiferDir() bool { return len(Cfg.AquiferDir) != 0 } func SetAquiferDir(aquiferDir string) error { - viper.Set("AquiferDir", aquiferDir) + viper.Set(ConfigNameAquiferDir, aquiferDir) err := viper.WriteConfig() if err != nil { @@ -98,12 +107,14 @@ func SetAquiferDir(aquiferDir string) error { return viper.Unmarshal(&Cfg) } +// aquifer repo url + func hasAquiferRepoURL() bool { return len(Cfg.AquiferRepoURL) != 0 } func SetAquiferRepoURL(aquiferRepoURL string) error { - viper.Set("AquiferRepoURL", aquiferRepoURL) + viper.Set(ConfigNameAquiferRepoURL, aquiferRepoURL) err := viper.WriteConfig() if err != nil { @@ -113,12 +124,14 @@ func SetAquiferRepoURL(aquiferRepoURL string) error { return viper.Unmarshal(&Cfg) } +// github username + func hasGitHubUsername() bool { return len(Cfg.GitHubUsername) != 0 } func SetGitHubUsername(githubUsername string) error { - viper.Set("GitHubUsername", githubUsername) + viper.Set(ConfigNameGithubUsername, githubUsername) err := viper.WriteConfig() if err != nil { @@ -128,12 +141,14 @@ func SetGitHubUsername(githubUsername string) error { return viper.Unmarshal(&Cfg) } +// github access token + func hasGitHubAccessToken() bool { return len(Cfg.GitHubAccessToken) != 0 } func SetGitHubAccessToken(githubAccessToken string) error { - viper.Set("GitHubAccessToken", githubAccessToken) + viper.Set(ConfigNameGithubAccessToken, githubAccessToken) err := viper.WriteConfig() if err != nil { diff --git a/internal/service/aquifer/aquifer.go b/internal/service/aquifer/aquifer.go index dd8ffda..5838b38 100644 --- a/internal/service/aquifer/aquifer.go +++ b/internal/service/aquifer/aquifer.go @@ -32,7 +32,7 @@ type Aquifer struct { Uninstall map[string]Plan } -func Install(pkgName string) error { +func Install(aquiferDir, pkgName string) error { err := config.SetCfgWithSurvey( config.SetAquiferDirWithSurvey, ) @@ -40,7 +40,7 @@ func Install(pkgName string) error { return err } - aquiferPath, err := getAquiferPath(config.Cfg.AquiferDir, pkgName) + aquiferPath, err := getAquiferPath(aquiferDir, pkgName) if err != nil { return err }