From 8906b4aa0194b4bca7caa3a0e10c7d80de44ece6 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 6 Dec 2021 15:48:55 +0100 Subject: [PATCH] Fix overwrite not working --- actions/client.go | 16 ++++++++++++++-- config/config.go | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/actions/client.go b/actions/client.go index e8ca2ab..db5617a 100644 --- a/actions/client.go +++ b/actions/client.go @@ -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 diff --git a/config/config.go b/config/config.go index 0caff72..b822995 100644 --- a/config/config.go +++ b/config/config.go @@ -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