Skip to content

Commit

Permalink
Fix exposed volumes & add var to customise cli datadir (#87)
Browse files Browse the repository at this point in the history
* Fix config init & customize cli datadir

* Fix exposed volumes

* Fix

* Fix

* Fix
  • Loading branch information
altafan authored Feb 13, 2024
1 parent dd0b884 commit ea1aaed
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ ENV OCEAN_CLI_DATADIR=/app/data/ocean
ENV PATH="/app:${PATH}"

# Expose volume containing all `oceand` data
VOLUME /app/data
VOLUME /app/data/oceand
VOLUME /app/data/ocean

# Expose ports of grpc server and profiler
EXPOSE 18000
Expand Down
13 changes: 6 additions & 7 deletions cmd/ocean/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ var (

func init() {
configInitCmd.Flags().StringVar(
&rpcServer, "rpcserver", initialState["rpcserver"],
&rpcServer, "rpcserver", initialState()["rpcserver"],
"address of the ocean wallet to connect to",
)
configInitCmd.Flags().BoolVar(
&noTLS, "no-tls", false,
"this must be set if the ocean wallet has TLS disabled",
)
configInitCmd.Flags().StringVar(
&tlsCertPath, "tls-cert-path", initialState["tls_cert_path"],
&tlsCertPath, "tls-cert-path", initialState()["tls_cert_path"],
"the path of the TLS certificate file to use to connect to the ocean "+
"wallet if it has TLS enabled",
)
Expand All @@ -59,15 +59,15 @@ func configSet(cmd *cobra.Command, args []string) error {
value := args[1]

// Prevent setting anything that is not part of the state.
if _, ok := initialState[key]; !ok {
if _, ok := initialState()[key]; !ok {
return nil
}

partialState := map[string]string{key: value}
if key == "no_tls" {
partialState["tls_cert_path"] = ""
if val, _ := strconv.ParseBool(value); !val {
partialState["tls_cert_path"] = initialState["tls_cert_path"]
partialState["tls_cert_path"] = initialState()["tls_cert_path"]
}
}
if key == "tls_cert_path" {
Expand All @@ -87,10 +87,9 @@ func configSet(cmd *cobra.Command, args []string) error {
}

func configInit(cmd *cobra.Command, args []string) error {
if _, err := getState(); err != nil {
return err
if noTLS {
tlsCertPath = ""
}

if err := setState(map[string]string{
"rpcserver": rpcServer,
"no_tls": strconv.FormatBool(noTLS),
Expand Down
38 changes: 32 additions & 6 deletions cmd/ocean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import (
"github.com/spf13/cobra"
)

const (
datadirKey = "OCEAN_CLI_DATADIR"
dbFile = "state.json"
)

var (
version = "dev"
commit = "none"
date = "unknown"

walletDatadir = btcutil.AppDataDir("oceand", false)
initialState = map[string]string{
"rpcserver": "localhost:18000",
"no_tls": strconv.FormatBool(false),
"tls_cert_path": filepath.Join(walletDatadir, "tls", "cert.pem"),
}
datadir = btcutil.AppDataDir("ocean-cli", false)
statePath string

rootCmd = &cobra.Command{
Use: "ocean",
Expand All @@ -36,6 +37,8 @@ var (
)

func init() {
initCLIEnv()

rootCmd.AddCommand(configCmd, walletCmd, accountCmd, txCmd)
}

Expand All @@ -45,3 +48,26 @@ func main() {
os.Exit(1)
}
}

func initCLIEnv() {
dir := cleanAndExpandPath(os.Getenv(datadirKey))
if len(dir) > 0 {
datadir = dir
}

statePath = filepath.Join(datadir, dbFile)

}

func initialState() map[string]string {
dir := cleanAndExpandPath(os.Getenv(datadirKey))
if len(dir) > 0 {
datadir = dir
}

return map[string]string{
"rpcserver": "localhost:18000",
"no_tls": strconv.FormatBool(false),
"tls_cert_path": filepath.Join(datadir, "tls", "cert.pem"),
}
}
5 changes: 2 additions & 3 deletions cmd/ocean/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func getState() (map[string]string, error) {
if !os.IsNotExist(err) {
return nil, err
}
if err := writeState(initialState); err != nil {
if err := writeState(initialState()); err != nil {
return nil, err
}
return initialState, nil
return initialState(), nil
}

data := map[string]string{}
Expand All @@ -120,7 +120,6 @@ func setState(partialState map[string]string) error {
}

func writeState(state map[string]string) error {

dir := filepath.Dir(statePath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
Expand Down
5 changes: 0 additions & 5 deletions cmd/ocean/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import (
"context"
"fmt"
"io"
"path/filepath"

"github.com/btcsuite/btcd/btcutil"
"github.com/spf13/cobra"
pb "github.com/vulpemventures/ocean/api-spec/protobuf/gen/go/ocean/v1"
)

var (
datadir = btcutil.AppDataDir("ocean-cli", false)
statePath = filepath.Join(datadir, "state.json")

mnemonic,
password,
oldPassword,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func newRpcClient(

useSSL := strings.HasPrefix(addr, "https")
if useSSL {
// #nosec
t := &http.Transport{
// #nosec G402
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient = &http.Client{Transport: t}
Expand Down

0 comments on commit ea1aaed

Please sign in to comment.