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