Skip to content
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

Add edit config command #14

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1 as builder
FROM golang:1 AS builder

COPY . /app
WORKDIR /app
Expand Down
4 changes: 2 additions & 2 deletions cmd/certs/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generates a full set of certificates for chia-blockchain",
Use: "generate",
Short: "Generates a full set of certificates for chia-blockchain",
Example: "chia-tools certs generate --output ~/.chia/mainnet/config/ssl",
Run: func(cmd *cobra.Command, args []string) {
err := tls.GenerateAllCerts(viper.GetString("cert-output"))
Expand Down
82 changes: 82 additions & 0 deletions cmd/config/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package config

import (
"os"
"path"

"github.com/chia-network/go-chia-libs/pkg/config"
"github.com/chia-network/go-modules/pkg/slogs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

// editCmd generates a new chia config
var editCmd = &cobra.Command{
Use: "edit",
Short: "Edit an existing chia configuration file",
Example: `chia-tools config edit ~/.chia/mainnet/config/config.yaml --set full_node.port=58444 --set full_node.target_peer_count=10

# The following version will discover the config file by inspecting CHIA_ROOT or using the default CHIA_ROOT
chia-tools config edit --set full_node.port=58444 --set full_node.target_peer_count=10`,
Run: func(cmd *cobra.Command, args []string) {
var cfgPath string

chiaRoot, err := config.GetChiaRootPath()
if err != nil {
slogs.Logr.Fatal("Unable to determine CHIA_ROOT", "error", err)
}

if len(args) > 1 {
slogs.Logr.Fatal("Unexpected number of arguments provided")
} else if len(args) == 1 {
// Use the provided config path
cfgPath = args[0]
} else {
// Use default chia root
cfgPath = path.Join(chiaRoot, "config", "config.yaml")
}

cfg, err := config.LoadConfigAtRoot(cfgPath, chiaRoot)
if err != nil {
slogs.Logr.Fatal("error loading chia config", "error", err)
}

err = cfg.FillValuesFromEnvironment()
if err != nil {
slogs.Logr.Fatal("error filling values from environment", "error", err)
}

valuesToSet := viper.GetStringMapString("edit-set")
for path, value := range valuesToSet {
pathMap := config.ParsePathsFromStrings([]string{path}, false)
var key string
var pathSlice []string
for key, pathSlice = range pathMap {
break
}
err = cfg.SetFieldByPath(pathSlice, value)
if err != nil {
slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err)
}
}

out, err := yaml.Marshal(cfg)
if err != nil {
slogs.Logr.Fatal("error marshalling config", "error", err)
}

err = os.WriteFile(cfgPath, out, 0655)
if err != nil {
slogs.Logr.Fatal("error writing output file", "error", err)
}
},
}

func init() {
editCmd.PersistentFlags().StringToStringP("set", "s", nil, "Paths and values to set in the config")

cobra.CheckErr(viper.BindPFlag("edit-set", editCmd.PersistentFlags().Lookup("set")))

configCmd.AddCommand(editCmd)
}
4 changes: 2 additions & 2 deletions cmd/datalayer/deletemirrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

// deleteMirrorsCmd Deletes all owned mirrors for all datalayer subscriptions
var deleteMirrorsCmd = &cobra.Command{
Use: "delete-mirrors",
Short: "Deletes all owned mirrors for all datalayer subscriptions",
Use: "delete-mirrors",
Short: "Deletes all owned mirrors for all datalayer subscriptions",
Example: "chia-tools data delete-mirrors --all\nchia-tools data delete-mirrors --id abcd1234",
PreRunE: func(cmd *cobra.Command, args []string) error {
all := viper.GetBool("delete-mirror-all")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/chia-network/chia-tools
go 1.22.4

require (
github.com/chia-network/go-chia-libs v0.9.1
github.com/chia-network/go-chia-libs v0.9.2
github.com/chia-network/go-modules v0.0.5
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/chia-network/go-chia-libs v0.9.1 h1:RAoCnnAl0hccUw6ue/9OsoYxFLlUiXpNy5OPDUDqUQQ=
github.com/chia-network/go-chia-libs v0.9.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
github.com/chia-network/go-chia-libs v0.9.2 h1:WWfiKoCA9zpoq4JgbX/l9cCcdJ4wQeWdc1iG29Fa8/E=
github.com/chia-network/go-chia-libs v0.9.2/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
github.com/chia-network/go-modules v0.0.5 h1:5luTVlP6RgBXodnFcWFBk2sLdJn+6vQ4wObim683C7c=
github.com/chia-network/go-modules v0.0.5/go.mod h1:5AiYBxQSvf2aFSOizTqFXXSeb9AucZWrWmRCVwUMO3A=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down