-
Notifications
You must be signed in to change notification settings - Fork 0
[Feature] init config #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb8f4c9
add logic for init config
nkitlabs 606b71e
add test
nkitlabs 0bf1e63
fix test
nkitlabs dbf889d
implement config show and custom confg
fa2e916
fix golint
0d83598
add validate invalid toml field logic
7006d68
fix golint
ee89bbd
Fix loadconfig and test
31f55df
del comment
c2aa43f
fix comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| const ( | ||
| flagHome = "home" | ||
| flagFile = "file" | ||
| ) | ||
|
|
||
| func configInitFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command { | ||
| fileFlag(v, cmd) | ||
| return cmd | ||
| } | ||
|
|
||
| func fileFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command { | ||
| cmd.Flags().StringP(flagFile, "f", "", "fetch toml data from specified file") | ||
| if err := v.BindPFlag(flagFile, cmd.Flags().Lookup(flagFile)); err != nil { | ||
| panic(err) | ||
| } | ||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,13 +64,39 @@ func (a *App) InitLogger(configLogLevel string) error { | |
| return nil | ||
| } | ||
|
|
||
| func (a *App) configPath() string { | ||
| return path.Join(a.HomePath, "config", "config.toml") | ||
tanut32039 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // loadConfigFile reads config file into a.Config if file is present. | ||
| func (a *App) LoadConfigFile(ctx context.Context) error { | ||
| cfgPath := a.configPath() | ||
| if _, err := os.Stat(cfgPath); err != nil { | ||
| // don't return error if file doesn't exist | ||
| return nil | ||
| } | ||
|
|
||
| // read the config file bytes | ||
| file, err := os.ReadFile(cfgPath) | ||
| if err != nil { | ||
| return fmt.Errorf("error reading file: %w", err) | ||
| } | ||
|
|
||
| // unmarshall them into the struct | ||
| cfg := &Config{} | ||
| err = toml.Unmarshal(file, cfg) | ||
| if err != nil { | ||
| return fmt.Errorf("error unmarshalling config: %w", err) | ||
| } | ||
nkitlabs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // save configuration | ||
| a.Config = cfg | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // InitConfigFile initializes the configuration to the given path. | ||
| func (a *App) InitConfigFile(homePath string) error { | ||
| func (a *App) InitConfigFile(homePath string, customFilePath string) error { | ||
| cfgDir := path.Join(homePath, configFolderName) | ||
| cfgPath := path.Join(cfgDir, configFileName) | ||
|
|
||
|
|
@@ -96,14 +122,25 @@ func (a *App) InitConfigFile(homePath string) error { | |
| } | ||
| } | ||
|
|
||
| var cfg Config | ||
| var err error | ||
| switch { | ||
| case customFilePath != "": | ||
| cfg, err = LoadConfig(customFilePath) // Initialize with CustomConfig if file is provided | ||
| if err != nil { | ||
| return fmt.Errorf("LoadConfig file %v error %v", customFilePath, err) | ||
| } | ||
| default: | ||
| cfg = DefaultConfig() // Initialize with DefaultConfig if no file is provided | ||
| } | ||
|
|
||
|
||
| // Create the file and write the default config to the given location. | ||
| f, err := os.Create(cfgPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| cfg := DefaultConfig() | ||
| b, err := toml.Marshal(cfg) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -116,6 +153,23 @@ func (a *App) InitConfigFile(homePath string) error { | |
| return nil | ||
| } | ||
|
|
||
| // Get config file from given home path. | ||
| func (a *App) GetConfigFile(homePath string) (string, error) { | ||
tanut32039 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cfgPath := path.Join(homePath, "config", "config.toml") | ||
| if _, err := os.Stat(cfgPath); os.IsNotExist(err) { | ||
| if _, err := os.Stat(homePath); os.IsNotExist(err) { | ||
| return "", fmt.Errorf("home path does not exist: %s", homePath) | ||
| } | ||
| return "", fmt.Errorf("config does not exist: %s", cfgPath) | ||
| } | ||
|
|
||
| out, err := toml.Marshal(a.Config) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return string(out), nil | ||
| } | ||
|
|
||
| // Start starts the tunnel relayer program. | ||
| func (a *App) Start(ctx context.Context, tunnelIDs []uint64) error { | ||
| // initialize band client | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.