forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce top-level config package (sourcenetwork#389)
- Loading branch information
1 parent
4b4ff4f
commit 3520fce
Showing
26 changed files
with
1,360 additions
and
495 deletions.
There are no files selected for viewing
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/sourcenetwork/defradb/config" | ||
"github.com/sourcenetwork/defradb/logging" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var reinitialize bool | ||
|
||
var initCmd = &cobra.Command{ | ||
Use: "init [rootdir]", | ||
Short: "Initialize DefraDB's root directory", | ||
Long: `Initialize a directory for DefraDB's configuration and data at the given path.`, | ||
// Load a default configuration, considering env. variables and CLI flags. | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
err := cfg.LoadWithoutRootDir() | ||
if err != nil { | ||
log.FatalE(context.Background(), "Failed to load config", err) | ||
} | ||
loggingConfig, err := cfg.GetLoggingConfig() | ||
if err != nil { | ||
log.FatalE(context.Background(), "Failed to get logging config", err) | ||
} | ||
logging.SetConfig(loggingConfig) | ||
}, | ||
Args: cobra.RangeArgs(0, 1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := context.Background() | ||
if len(args) > 0 { | ||
rootDirParam = args[0] | ||
} | ||
rootDir, exists, err := config.GetRootDir(rootDirParam) | ||
if err != nil { | ||
log.FatalE(ctx, "Failed to get root dir", err) | ||
} | ||
if exists { | ||
if reinitialize { | ||
log.Info(ctx, "Reinitializing root directory", logging.NewKV("path", rootDir)) | ||
err := os.RemoveAll(rootDir) | ||
if err != nil { | ||
log.FatalE(ctx, "Failed to delete root directory", err) | ||
} | ||
err = config.CreateRootDirWithDefaultConfig(rootDir) | ||
if err != nil { | ||
log.FatalE(ctx, "Failed to create root directory", err) | ||
} | ||
} else { | ||
log.Warn(ctx, "Root directory already exists. Consider using --reinitialize", logging.NewKV("path", rootDir)) | ||
} | ||
} else { | ||
err = config.CreateRootDirWithDefaultConfig(rootDir) | ||
if err != nil { | ||
log.FatalE(ctx, "Failed to create root directory", err) | ||
} | ||
log.Info(ctx, "Created DefraDB directory", logging.NewKV("path", rootDir)) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(initCmd) | ||
|
||
initCmd.Flags().BoolVar( | ||
&reinitialize, "reinitialize", false, | ||
"reinitialize the root directory", | ||
) | ||
} |
This file contains 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.