Merge pull request 'Fix overwrite not working' (#15) from bug/12 into master

Reviewed-on: https://gitea.benny.dog/torjus/ezshare/pulls/15
This commit is contained in:
Torjus Håkestad 2021-12-06 14:49:58 +00:00
commit 67014f7af7
2 changed files with 19 additions and 2 deletions

View File

@ -176,6 +176,7 @@ func ActionClientDelete(c *cli.Context) error {
}
func ActionClientLogin(c *cli.Context) error {
// Ensure config-dir exists
if err := config.CreateDefaultConfigDir(); err != nil {
return err
}
@ -184,6 +185,7 @@ func ActionClientLogin(c *cli.Context) error {
return err
}
// Check if config already exists
if _, err := os.Stat(configFilePath); !errors.Is(err, os.ErrNotExist) {
if err == nil {
if !c.Bool("overwrite") {
@ -242,6 +244,7 @@ func ActionClientLogin(c *cli.Context) error {
return cli.Exit(fmt.Sprintf("unable to decode metadata response: %s", err), 1)
}
// Prompt for username and password
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("username: ")
scanner.Scan()
@ -254,10 +257,12 @@ func ActionClientLogin(c *cli.Context) error {
}
password := string(passwordBytes)
// Setup certificate credentials
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(serverCert) {
return cli.Exit(fmt.Sprintf("unable to use server certificate: %s", err), 1)
}
// Generate temporary self-signed cert
keyBytes, certBytes, err := certs.GenCACert()
if err != nil {
@ -278,6 +283,7 @@ func ActionClientLogin(c *cli.Context) error {
creds := credentials.NewTLS(&tls.Config{RootCAs: certPool, Certificates: []tls.Certificate{clientCert}})
// Connect to grpc-endpoint
verbosePrint(c, fmt.Sprintf("dialing grpc at %s", md.GRPCEndpoint))
conn, err := grpc.DialContext(c.Context, md.GRPCEndpoint, grpc.WithTransportCredentials(creds))
if err != nil {
@ -332,8 +338,14 @@ func ActionClientLogin(c *cli.Context) error {
cfg.Client.DefaultServer = md.GRPCEndpoint
cfg.Client.ServerCertPath = serverCertPath
if err := cfg.ToDefaultFile(); err != nil {
return cli.Exit(fmt.Sprintf("unable write config to file: %s", err), 1)
verbosePrint(c, fmt.Sprintf("Writing config to %s", configFilePath))
f, err := os.Create(configFilePath)
if err != nil {
return cli.Exit(fmt.Sprintf("Unable to open config-file for writing: %s", err), 1)
}
defer f.Close()
if err := cfg.ToWriter(f); err != nil {
return cli.Exit(fmt.Sprintf("Unable write config to file: %s", err), 1)
}
return nil

View File

@ -234,6 +234,11 @@ func DefaultConfigFilePath() (string, error) {
}
func (c *Config) ToWriter(w io.Writer) error {
encoder := toml.NewEncoder(w)
return encoder.Encode(c)
}
func (c *Config) ToDefaultFile() error {
if err := CreateDefaultConfigDir(); err != nil {
return err