Skip to content

Commit

Permalink
feat: Introduce top-level config package
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed May 10, 2022
1 parent 1ea959d commit ea50dc8
Show file tree
Hide file tree
Showing 22 changed files with 1,173 additions and 449 deletions.
16 changes: 7 additions & 9 deletions cli/defradb/cmd/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/defradb/cmd/blocks_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import (
"fmt"
"io"
"net/http"
"strings"

"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]

res, err := http.Get(fmt.Sprintf("%s/blocks/get/%s", dbaddr, cid))
res, err := http.Get(fmt.Sprintf("%s/blocks/get/%s", cfg.API.AddressToURL(), cid))
if err != nil {
log.ErrorE(ctx, "request failed", err)
return
Expand Down
129 changes: 0 additions & 129 deletions cli/defradb/cmd/config.go

This file was deleted.

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

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

// dumpCmd represents the dump command
Expand All @@ -28,17 +25,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
}

res, err := http.Get(fmt.Sprintf("%s/dump", dbaddr))
res, err := http.Get(fmt.Sprintf("%s/dump", cfg.API.AddressToURL()))
if err != nil {
log.ErrorE(ctx, "request failed", err)
return
Expand Down
73 changes: 73 additions & 0 deletions cli/defradb/cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 cmd

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.`,
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 {
rootDir = args[0]
}
rootDir, exists := config.GetRootDir(rootDir)
if exists {
if reinitialize {
log.Info(ctx, "Reinitializing root directory", logging.NewKV("path", rootDir))
err := os.RemoveAll(rootDir)
if err != nil {
log.Fatal(ctx, "Failed to delete root directory", logging.NewKV("error", err))
}
config.CreateRootDirWithDefaultConfig(rootDir)
} else {
log.Warn(ctx, "Root directory already exists. Consider using --reinitialize", logging.NewKV("path", rootDir))
}
} else {
config.CreateRootDirWithDefaultConfig(rootDir)
log.Info(ctx, "Created DefraDB root 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/defradb/cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (
"fmt"
"io"
"net/http"
"strings"

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

// pingCmd represents the ping command
Expand All @@ -28,18 +25,9 @@ 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...")
res, err := http.Get(fmt.Sprintf("%s/ping", dbaddr))
res, err := http.Get(fmt.Sprintf("%s/ping", cfg.API.AddressToURL()))
if err != nil {
log.ErrorE(ctx, "request failed", err)
return
Expand Down
18 changes: 1 addition & 17 deletions cli/defradb/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@ import (
"io"
"net/http"
"net/url"
"strings"

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

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

// queryCmd represents the query command
Expand All @@ -42,15 +35,6 @@ the additional documentation found at: https://hackmd.io/@source/BksQY6Qfw.
`,
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 query argument")
Expand All @@ -60,7 +44,7 @@ the additional documentation found at: https://hackmd.io/@source/BksQY6Qfw.
log.Error(ctx, "missing query")
return
}
endpointStr := fmt.Sprintf("%s/graphql", dbaddr)
endpointStr := fmt.Sprintf("%s/graphql", cfg.API.AddressToURL())
endpoint, err := url.Parse(endpointStr)
if err != nil {
log.FatalE(ctx, "", err)
Expand Down
Loading

0 comments on commit ea50dc8

Please sign in to comment.