Add command to initialize client-config
This commit is contained in:
@@ -3,8 +3,10 @@ package config
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -171,5 +173,42 @@ func (cc *ClientConfig) Creds() (credentials.TransportCredentials, error) {
|
||||
RootCAs: certPool,
|
||||
}
|
||||
return credentials.NewTLS(config), nil
|
||||
|
||||
}
|
||||
|
||||
func (c *Config) ToDefaultFile() error {
|
||||
userConfigDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
configDirPath := filepath.Join(userConfigDir, "ezshare")
|
||||
info, err := os.Stat(configDirPath)
|
||||
if err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
if err := os.Mkdir(configDirPath, 0755); err != nil {
|
||||
return fmt.Errorf("unable to create config-dir: %w", err)
|
||||
}
|
||||
} else {
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("config-directory is not a directory")
|
||||
}
|
||||
}
|
||||
|
||||
configFilePath := filepath.Join(configDirPath, "ezshare.toml")
|
||||
_, err = os.Stat(configFilePath)
|
||||
if err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return fmt.Errorf("error stating config-file: %w", err)
|
||||
}
|
||||
|
||||
f, err := os.Create(configFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create config-file: %w", err)
|
||||
}
|
||||
encoder := toml.NewEncoder(f)
|
||||
fmt.Printf("Writing config to '%s'", configFilePath)
|
||||
return encoder.Encode(c)
|
||||
}
|
||||
return fmt.Errorf("config-file already exists")
|
||||
}
|
||||
|
Reference in New Issue
Block a user