From 9c56cf4687250ded908ee7a3f5d56596b67a5fee Mon Sep 17 00:00:00 2001 From: "takahiro.tominaga" Date: Tue, 7 Feb 2023 23:11:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B0=A1=E6=98=93=E7=9A=84=E3=81=AA?= =?UTF-8?q?=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3=E3=82=B3=E3=83=9E=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/login.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 ++ util/config.go | 12 ++++--- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 cmd/login.go diff --git a/cmd/login.go b/cmd/login.go new file mode 100644 index 0000000..b8dba0d --- /dev/null +++ b/cmd/login.go @@ -0,0 +1,89 @@ +package cmd + +import ( + "bufio" + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/android-project-46group/sgi-cli/util" + "github.com/spf13/cobra" +) + +// Add login command to rootCmd +func addLoginCmd() { + loginCmd := &cobra.Command{ + Use: "login", + Short: "login", + Long: `For now, it just receives information from the user and stores it in a file.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Please provide your login information.\n\n") + err := login() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, + } + + rootCmd.AddCommand(loginCmd) +} + +// login is the main logic of the login function. +// +// Currently, it only reads the information entered by the user +// and saves it to the file. +func login() error { + fmt.Printf(" BaseURL: ") + url, err := readFromTerminal() + if err != nil { + return err + } + + fmt.Printf(" APIKey: ") + key, err := readFromTerminal() + if err != nil { + return err + } + + ac := util.Account{ + BaseURL: url, + APIKey: key, + } + acJson, err := json.MarshalIndent(ac, "", " ") + if err != nil { + return err + } + + home, err := os.UserHomeDir() + if err != nil { + return err + } + + // Make the [util.CliDir] directory if needed. + err = os.Mkdir(filepath.Join(home, util.CliDir), os.ModePerm) + if err != nil && !errors.Is(err, os.ErrExist) { + return err + } + + joined := filepath.Join(home, util.CliDir, util.AccountFile) + if err = os.WriteFile(joined, acJson, 0644); err != nil { + return err + } + fmt.Printf("\nLogin succeeded!\n") + fmt.Printf("Your information is stored at: %s\n", joined) + return nil +} + +// readFromTerminal reads user's input from the terminal. +func readFromTerminal() (string, error) { + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + return "", err + } + return strings.TrimSuffix(input, "\n"), nil +} diff --git a/cmd/root.go b/cmd/root.go index e915a58..49b278a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,6 +23,8 @@ var rootCmd = &cobra.Command{ func Execute(config util.Config, api api.ApiCaller) { addVersionCmd(config.Version) + addLoginCmd() + // Before commands which need API call, validation will be executed. addMemberCmd(api, config) addGroupCmd(api, config) diff --git a/util/config.go b/util/config.go index ced2a8f..38a4c52 100644 --- a/util/config.go +++ b/util/config.go @@ -31,12 +31,16 @@ type Account struct { } var ( - // Path to an account setting file from HOME directory - accountPath = ".sgi/account.json" + // Path to Working directory related to CLI tools. + // The path is described relative to the HOME directory. + CliDir = ".sgi" + + // Name of the file where the account information is stored. + AccountFile = "account.json" ) // NewConfig returns a new [Config] instance. -// The configuration value is obtained from accountPath under the ${HOME} directory. +// The configuration value is obtained from [AccountFile] under the ${HOME} directory. // You need to pass the cli version as an argument. func NewConfig(version string) (Config, error) { @@ -48,7 +52,7 @@ func NewConfig(version string) (Config, error) { if err != nil { return cfg, err } - joined := filepath.Join(home, accountPath) + joined := filepath.Join(home, CliDir, AccountFile) f, err := os.Open(joined) if err != nil {