Skip to content

Commit

Permalink
feat: Introduce top-level config package (sourcenetwork#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis authored Jun 29, 2022
1 parent 4b4ff4f commit 3520fce
Show file tree
Hide file tree
Showing 26 changed files with 1,360 additions and 495 deletions.
16 changes: 7 additions & 9 deletions cli/addreplicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
netclient "github.com/sourcenetwork/defradb/net/api/client"
)

var (
// Commented because it is deadcode, for linter.
// queryStr string
)

// queryCmd represents the query command
var addReplicatorCmd = &cobra.Command{
Use: "addreplicator",
Expand All @@ -37,7 +32,6 @@ for the p2p data sync system.
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
logging.SetConfig(config.Logging.toLogConfig())

// get args
collection := args[0]
Expand All @@ -51,15 +45,19 @@ for the p2p data sync system.
"Adding replicator for collection",
logging.NewKV("PeerAddress", peerAddr),
logging.NewKV("Collection", collection),
logging.NewKV("RPCAddress", rpcAddr))
logging.NewKV("RPCAddress", cfg.Net.RPCAddress))

cred := insecure.NewCredentials()
client, err := netclient.NewClient(rpcAddr, grpc.WithTransportCredentials(cred))
client, err := netclient.NewClient(cfg.Net.RPCAddress, grpc.WithTransportCredentials(cred))
if err != nil {
log.FatalE(ctx, "Couldn't create RPC client", err)
}

ctx, cancel := context.WithTimeout(ctx, rpcTimeout)
rpcTimeoutDuration, err := cfg.Net.RPCTimeoutDuration()
if err != nil {
log.FatalE(ctx, "Failed to parse RPC timeout duration", err)
}
ctx, cancel := context.WithTimeout(ctx, rpcTimeoutDuration)
defer cancel()

pid, err := client.AddReplicator(ctx, collection, peerAddr)
Expand Down
13 changes: 1 addition & 12 deletions cli/blocks_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import (
"context"
"io"
"net/http"
"strings"

httpapi "github.com/sourcenetwork/defradb/api/http"
"github.com/sourcenetwork/defradb/logging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getCmd represents the get command
Expand All @@ -28,22 +26,13 @@ var getCmd = &cobra.Command{
Short: "Get a block by its CID from the blockstore",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
logging.SetConfig(config.Logging.toLogConfig())

dbaddr := viper.GetString("database.address")
if dbaddr == "" {
log.Error(ctx, "No database URL provided")
}
if !strings.HasPrefix(dbaddr, "http") {
dbaddr = "http://" + dbaddr
}

if len(args) != 1 {
log.Fatal(ctx, "Needs a single CID")
}
cid := args[0]

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.BlocksPath, cid)
endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.BlocksPath, cid)
if err != nil {
log.ErrorE(ctx, "Join paths failed", err)
return
Expand Down
3 changes: 1 addition & 2 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ var clientCmd = &cobra.Command{
Use: "client",
Short: "Interact with a running DefraDB node as a client",
Long: `Interact with a running DefraDB node as a client.
This command allows you to execute queries, add schema
types, and run debug routines.`,
This command allows you to execute queries, add schema types, and run debug routines.`,
}

func init() {
Expand Down
129 changes: 0 additions & 129 deletions cli/config.go

This file was deleted.

14 changes: 1 addition & 13 deletions cli/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import (
"fmt"
"io"
"net/http"
"strings"

httpapi "github.com/sourcenetwork/defradb/api/http"
"github.com/sourcenetwork/defradb/logging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// dumpCmd represents the dump command
Expand All @@ -29,17 +26,8 @@ var dumpCmd = &cobra.Command{
Short: "Dumps the state of the entire database (server side)",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
logging.SetConfig(config.Logging.toLogConfig())

dbaddr := viper.GetString("database.address")
if dbaddr == "" {
log.Error(ctx, "No database URL provided")
}
if !strings.HasPrefix(dbaddr, "http") {
dbaddr = "http://" + dbaddr
}

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.DumpPath)
endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.DumpPath)
if err != nil {
log.ErrorE(ctx, "Join paths failed", err)
return
Expand Down
81 changes: 81 additions & 0 deletions cli/init.go
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",
)
}
14 changes: 1 addition & 13 deletions cli/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import (
"fmt"
"io"
"net/http"
"strings"

httpapi "github.com/sourcenetwork/defradb/api/http"
"github.com/sourcenetwork/defradb/logging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// pingCmd represents the ping command
Expand All @@ -29,19 +26,10 @@ var pingCmd = &cobra.Command{
Short: "Ping defradb to test an API connection",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
logging.SetConfig(config.Logging.toLogConfig())

dbaddr := viper.GetString("database.address")
if dbaddr == "" {
log.Error(ctx, "No database URL provided")
}
if !strings.HasPrefix(dbaddr, "http") {
dbaddr = "http://" + dbaddr
}

log.Info(ctx, "Sending ping...")

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.PingPath)
endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.PingPath)
if err != nil {
log.ErrorE(ctx, "Join paths failed", err)
return
Expand Down
Loading

0 comments on commit 3520fce

Please sign in to comment.