Skip to content

Commit

Permalink
Merge branch 'v2-maint' into integerTypeCasting
Browse files Browse the repository at this point in the history
  • Loading branch information
jack authored Feb 7, 2023
2 parents a105e80 + 3d46fd3 commit afcb85a
Show file tree
Hide file tree
Showing 28 changed files with 478 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: [1.17.x, 1.18.x, 1.19.x]
go: [1.18.x, 1.19.x]
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
steps:
Expand Down
3 changes: 1 addition & 2 deletions altsrc/json_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -318,7 +317,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *tes
}

func writeTempFile(t *testing.T, name string, content string) func() {
if err := ioutil.WriteFile(name, []byte(content), 0666); err != nil {
if err := os.WriteFile(name, []byte(content), 0666); err != nil {
t.Fatalf("cannot write %q: %v", name, err)
}
return func() {
Expand Down
3 changes: 1 addition & 2 deletions altsrc/json_source_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"time"

Expand Down Expand Up @@ -40,7 +39,7 @@ func NewJSONSourceFromFile(f string) (InputSourceContext, error) {
// NewJSONSourceFromReader returns an InputSourceContext suitable for
// retrieving config variables from an io.Reader that returns JSON data.
func NewJSONSourceFromReader(r io.Reader) (InputSourceContext, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
19 changes: 9 additions & 10 deletions altsrc/toml_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand All @@ -12,7 +11,7 @@ import (
func TestCommandTomFileTest(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")
test := []string{"test-cmd", "--load", "current.toml"}
_ = set.Parse(test)
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestCommandTomFileTest(t *testing.T) {
func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "10")
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "10")
Expand Down Expand Up @@ -110,7 +109,7 @@ func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

test := []string{"test-cmd", "--load", "current.toml", "--test", "7"}
Expand Down Expand Up @@ -142,7 +141,7 @@ func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte(`[top]
_ = os.WriteFile("current.toml", []byte(`[top]
test = 15`), 0666)
defer os.Remove("current.toml")

Expand Down Expand Up @@ -175,7 +174,7 @@ func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

test := []string{"test-cmd", "--load", "current.toml"}
Expand Down Expand Up @@ -207,7 +206,7 @@ func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
defer os.Remove("current.toml")

test := []string{"test-cmd", "--load", "current.toml"}
Expand Down Expand Up @@ -239,7 +238,7 @@ func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "11")
Expand Down Expand Up @@ -273,7 +272,7 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "11")
Expand Down
19 changes: 9 additions & 10 deletions altsrc/yaml_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand All @@ -12,7 +11,7 @@ import (
func TestCommandYamlFileTest(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")
test := []string{"test-cmd", "--load", "current.yaml"}
_ = set.Parse(test)
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestCommandYamlFileTest(t *testing.T) {
func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

_ = os.Setenv("THE_TEST", "10")
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down Expand Up @@ -111,7 +110,7 @@ func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down Expand Up @@ -176,7 +175,7 @@ func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

test := []string{"test-cmd", "--load", "current.yaml"}
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down Expand Up @@ -241,7 +240,7 @@ func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

_ = os.Setenv("THE_TEST", "11")
Expand Down Expand Up @@ -275,7 +274,7 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down
8 changes: 4 additions & 4 deletions altsrc/yaml_file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package altsrc

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -68,21 +68,21 @@ func loadDataFrom(filePath string) ([]byte, error) {
if err != nil {
return nil, err
}
return ioutil.ReadAll(res.Body)
return io.ReadAll(res.Body)
default:
return nil, fmt.Errorf("scheme of %s is unsupported", filePath)
}
} else if u.Path != "" { // i dont have a host, but I have a path. I am a local file.
if _, notFoundFileErr := os.Stat(filePath); notFoundFileErr != nil {
return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath)
}
return ioutil.ReadFile(filePath)
return os.ReadFile(filePath)
} else if runtime.GOOS == "windows" && strings.Contains(u.String(), "\\") {
// on Windows systems u.Path is always empty, so we need to check the string directly.
if _, notFoundFileErr := os.Stat(filePath); notFoundFileErr != nil {
return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath)
}
return ioutil.ReadFile(filePath)
return os.ReadFile(filePath)
}

return nil, fmt.Errorf("unable to determine how to load from path %s", filePath)
Expand Down
33 changes: 16 additions & 17 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ type App struct {
// Treat all flags as normal arguments if true
SkipFlagParsing bool

didSetup bool
didSetup bool
separator separatorSpec

rootCommand *Command
}
Expand Down Expand Up @@ -216,15 +217,25 @@ func (a *App) Setup() {
})
}

if len(a.SliceFlagSeparator) != 0 {
a.separator.customized = true
a.separator.sep = a.SliceFlagSeparator
}

if a.DisableSliceFlagSeparator {
a.separator.customized = true
a.separator.disabled = true
}

var newCommands []*Command

for _, c := range a.Commands {
cname := c.Name
if c.HelpName != "" {
cname = c.HelpName
}
c.separator = a.separator
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, cname)

c.flagCategories = newFlagCategoriesFromFlags(c.Flags)
newCommands = append(newCommands, c)
}
Expand All @@ -250,24 +261,11 @@ func (a *App) Setup() {
}
sort.Sort(a.categories.(*commandCategories))

a.flagCategories = newFlagCategories()
for _, fl := range a.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
if cf.GetCategory() != "" {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
}
}
}
a.flagCategories = newFlagCategoriesFromFlags(a.Flags)

if a.Metadata == nil {
a.Metadata = make(map[string]interface{})
}

if len(a.SliceFlagSeparator) != 0 {
defaultSliceFlagSeparator = a.SliceFlagSeparator
}

disableSliceFlagSeparator = a.DisableSliceFlagSeparator
}

func (a *App) newRootCommand() *Command {
Expand All @@ -293,11 +291,12 @@ func (a *App) newRootCommand() *Command {
categories: a.categories,
SkipFlagParsing: a.SkipFlagParsing,
isRoot: true,
separator: a.separator,
}
}

func (a *App) newFlagSet() (*flag.FlagSet, error) {
return flagSet(a.Name, a.Flags)
return flagSet(a.Name, a.Flags, a.separator)
}

func (a *App) useShortOptionHandling() bool {
Expand Down
Loading

0 comments on commit afcb85a

Please sign in to comment.