Skip to content

Commit

Permalink
Merge pull request #355 from rusq/toml-apiconfig
Browse files Browse the repository at this point in the history
Toml apiconfig
  • Loading branch information
rusq authored Nov 17, 2024
2 parents 349b2c3 + e70aa42 commit 15bd8c7
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ __debug_bin
*.json
*.yml
*.yaml
*.toml
json/*
examples/*
experiments/*
Expand Down
15 changes: 4 additions & 11 deletions cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ import (
"io"
"os"

"github.com/BurntSushi/toml"
"github.com/go-playground/validator/v10"
"gopkg.in/yaml.v3"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/internal/network"
)

// schemaJSONpath is the path to the schema JSON file for the limits yaml
// configuration file.
// TODO: update once released
const schemaJSONpath = "https://raw.githubusercontent.com/rusq/slackdump/v3/cmd/slackdump/internal/apiconfig/schema.json"

var CmdConfig = &base.Command{
UsageLine: "slackdump config",
Short: "API configuration",
Expand Down Expand Up @@ -62,9 +57,8 @@ func Save(filename string, limits network.Limits) error {
// the global config.
func applyLimits(r io.Reader) (network.Limits, error) {
var limits network.Limits
dec := yaml.NewDecoder(r)
dec.KnownFields(true)
if err := dec.Decode(&limits); err != nil {
dec := toml.NewDecoder(r)
if _, err := dec.Decode(&limits); err != nil {
return network.Limits{}, err
}

Expand All @@ -78,8 +72,7 @@ func applyLimits(r io.Reader) (network.Limits, error) {
}

func writeLimits(w io.Writer, cfg network.Limits) error {
fmt.Fprintf(w, "# yaml-language-server: $schema=%s\n", schemaJSONpath)
return yaml.NewEncoder(w).Encode(cfg)
return toml.NewEncoder(w).Encode(cfg)
}

// printErrors prints configuration errors, if error is not nill and is of
Expand Down
87 changes: 47 additions & 40 deletions cmd/slackdump/internal/apiconfig/apiconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,52 @@ import (
)

const (
sampleLimitsYaml = `# yaml-language-server: $schema=https://raw.githubusercontent.com/rusq/slackdump/v3/cmd/slackdump/internal/apiconfig/schema.json
workers: 4
download_retries: 3
tier_2:
boost: 20
burst: 3
retries: 20
tier_3:
boost: 120
burst: 5
retries: 3
tier_4:
boost: 10
burst: 7
retries: 3
per_request:
conversations: 100
channels: 100
replies: 200
sampleLimitsYaml = `workers = 4
download_retries = 3
[tier_2]
boost = 20
burst = 3
retries = 20
[tier_3]
boost = 120
burst = 5
retries = 3
[tier_4]
boost = 10
burst = 7
retries = 3
[per_request]
conversations = 100
channels = 100
replies = 200
`
// workers set to 55 in this one, tier2.retries to 330
updatedConfigYaml = `workers: 55
download_retries: 3
tier_2:
boost: 20
burst: 1
retries: 330
tier_3:
boost: 120
burst: 1
retries: 3
tier_4:
boost: 10
burst: 1
retries: 3
per_request:
conversations: 100
channels: 100
replies: 200
updatedConfigYaml = `workers = 55
download_retries = 3
[tier_2]
boost = 20
burst = 3
retries = 330
[tier_3]
boost = 120
burst = 5
retries = 3
[tier_4]
boost = 10
burst = 7
retries = 3
[per_request]
conversations = 100
channels = 100
replies = 200
`
)

Expand Down Expand Up @@ -108,17 +115,17 @@ func Test_readConfig(t *testing.T) {
DownloadRetries: 3,
Tier2: network.TierLimit{
Boost: 20,
Burst: 1,
Burst: 3,
Retries: 330,
},
Tier3: network.TierLimit{
Boost: 120,
Burst: 1,
Burst: 5,
Retries: 3,
},
Tier4: network.TierLimit{
Boost: 10,
Burst: 1,
Burst: 7,
Retries: 3,
},
Request: network.RequestLimit{
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/apiconfig/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Allows to check the config for errors and invalid values.
Example:
slackdump config check myconfig.yaml
slackdump config check myconfig.toml
It will check for duplicate and unknown keys, and also ensure that values are
within the allowed boundaries.
Expand Down Expand Up @@ -58,7 +58,7 @@ func CheckFile(filename string) error {
}

func wizConfigCheck(ctx context.Context, cmd *base.Command, args []string) error {
f := filemgr.New(os.DirFS("."), ".", 15, "*.yaml", "*.yml")
f := filemgr.New(os.DirFS("."), ".", 15, "*.toml", "*.tml")
f.Focus()
f.ShowHelp = true
f.Style = filemgr.Style{
Expand Down
10 changes: 5 additions & 5 deletions cmd/slackdump/internal/apiconfig/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var CmdConfigNew = &base.Command{
Creates a new API configuration file containing default values. You will need
to specify the filename, for example:
slackdump config new myconfig.yaml
slackdump config new myconfig.toml
If the extension is omitted, ".yaml" is automatically appended to the filename.
If the extension is omitted, ".toml" is automatically appended to the filename.
`,
FlagMask: cfg.OmitAll,
PrintFlags: true,
Expand Down Expand Up @@ -100,11 +100,11 @@ func shouldOverwrite(filename string, override bool) bool {
return err != nil || override
}

// maybeFixExt checks if the extension is one of .yaml or .yml, and if not
// maybeFixExt checks if the extension is one of .toml or .tml, and if not
// appends it to the file.
func maybeFixExt(filename string) string {
if ext := filepath.Ext(filename); !(ext == ".yaml" || ext == ".yml") {
return maybeAppendExt(filename, ".yaml")
if ext := filepath.Ext(filename); !(ext == ".toml" || ext == ".tml") {
return maybeAppendExt(filename, ".toml")
}
return filename
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/slackdump/internal/apiconfig/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ func Test_maybeFixExt(t *testing.T) {
want string
}{
{
"already yaml",
args{filename: "lol.yaml"},
"lol.yaml",
"already toml",
args{filename: "lol.toml"},
"lol.toml",
},
{
"already yml",
args{filename: "lol.yml"},
"lol.yml",
"already tml",
args{filename: "lol.tml"},
"lol.tml",
},
{
"no extension",
args{filename: "foo"},
"foo.yaml",
"foo.toml",
},
{
"different extension",
args{filename: "foo.bar"},
"foo.bar.yaml",
"foo.bar.toml",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_shouldOverwrite(t *testing.T) {

func Test_runConfigNew(t *testing.T) {
dir := t.TempDir()
existingDir := filepath.Join(dir, "test.yaml")
existingDir := filepath.Join(dir, "test.toml")
if err := os.MkdirAll(existingDir, 0777); err != nil {
t.Fatal(err)
}
Expand All @@ -175,12 +175,12 @@ func Test_runConfigNew(t *testing.T) {
},
{
"file is created",
args{[]string{filepath.Join(dir, "sample.yml")}},
args{[]string{filepath.Join(dir, "sample.tml")}},
false,
true,
},
{
"directory test.yaml",
"directory test.toml",
args{[]string{existingDir}},
true,
true,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/rusq/slackdump/v3
go 1.23

require (
github.com/BurntSushi/toml v1.4.0
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403
github.com/ProtonMail/go-crypto v1.1.2
github.com/charmbracelet/bubbles v0.20.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403 h1:EtZwYyLbkEcIt+B//6sujwRCnHuTEK3qiSypAX5aJeM=
Expand Down

0 comments on commit 15bd8c7

Please sign in to comment.