Skip to content

Commit

Permalink
Merge branch 'issue/13'
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Feb 7, 2023
2 parents d1ce895 + 9c56cf4 commit e153c36
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
89 changes: 89 additions & 0 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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 {
Expand Down

0 comments on commit e153c36

Please sign in to comment.