Skip to content

Commit

Permalink
Merge pull request #6 from urfave/cli-v3-integration
Browse files Browse the repository at this point in the history
Integrate with urfave/cli/v3 APIs
  • Loading branch information
meatballhat authored Jun 23, 2023
2 parents 4f91f40 + 5442352 commit 26109e3
Show file tree
Hide file tree
Showing 48 changed files with 737 additions and 3,249 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: [1.18.x, 1.19.x]
go: [1.18.x, 1.19.x, 1.20.x]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- run: go test -v ./...
with:
fetch-depth: 0
- run: make
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.coverprofile
*.exe
*.orig
.*envrc
.envrc
.idea
/.local/
/site/
coverage.txt
vendor
6 changes: 6 additions & 0 deletions .testdata/alt-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"greet": {
"name": "Berry",
"enthusiasm": "eleven"
}
}
3 changes: 3 additions & 0 deletions .testdata/alt-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[greet]
name = "Berry"
enthusiasm = "eleven"
3 changes: 3 additions & 0 deletions .testdata/alt-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
greet:
name: Berry
enthusiasm: eleven
5 changes: 5 additions & 0 deletions .testdata/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"greet": {
"enthusiasm": 9001
}
}
2 changes: 2 additions & 0 deletions .testdata/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[greet]
enthusiasm = 9001
2 changes: 2 additions & 0 deletions .testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
greet:
enthusiasm: 9001
8 changes: 8 additions & 0 deletions .testdata/test_alt_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"water_fountain": {
"water": true
},
"phone_booth": {
"phone": false
}
}
5 changes: 5 additions & 0 deletions .testdata/test_alt_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[water_fountain]
water = true

[phone_booth]
phone = false
4 changes: 4 additions & 0 deletions .testdata/test_alt_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
water_fountain:
water: true
phone_booth:
phone: false
8 changes: 8 additions & 0 deletions .testdata/test_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"water_fountain": {
"water": false
},
"woodstock": {
"wood": false
}
}
5 changes: 5 additions & 0 deletions .testdata/test_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[water_fountain]
water = false

[woodstock]
wood = false
4 changes: 4 additions & 0 deletions .testdata/test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
water_fountain:
water: false
woodstock:
wood: false
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 urfave contributors
Copyright (c) 2023 urfave contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: all
all: vet test show-cover

.PHONY: vet
vet:
go vet -v ./...

.PHONY: test
test:
go test -v -cover -coverprofile=coverage.txt ./...

.PHONY: show-cover
show-cover:
go tool cover -func=coverage.txt
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# cli-altsrc
# Welcome to urfave/cli-altsrc/v3

Configuration source integration library for urfave/cli
[![Run Tests](https://github.com/urfave/cli-altsrc/actions/workflows/main.yml/badge.svg)](https://github.com/urfave/cli-altsrc/actions/workflows/main.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/urfave/cli-altsrc/v3.svg)](https://pkg.go.dev/github.com/urfave/cli-altsrc/v3)
[![Go Report Card](https://goreportcard.com/badge/github.com/urfave/cli-altsrc/v3)](https://goreportcard.com/report/github.com/urfave/cli-altsrc/v3)

urfave/cli-altsrc/v3 is an extended value source integration library for [urfave/cli/v3] with support for JSON,
YAML, and TOML. The primary reason for this to be a separate library is that third-party libraries are used for these
features which are otherwise not used throughout [urfave/cli/v3].

[urfave/cli/v3]: github.com/urfave/cli
106 changes: 106 additions & 0 deletions altsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package altsrc

import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"runtime"
"strings"
)

var (
Err = errors.New("urfave/cli-altsrc error")

isTracingOn = os.Getenv("URFAVE_CLI_TRACING") == "on"
)

func tracef(format string, a ...any) {
if !isTracingOn {
return
}

if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}

pc, file, line, _ := runtime.Caller(1)
cf := runtime.FuncForPC(pc)

fmt.Fprintf(
os.Stderr,
strings.Join([]string{
"## URFAVE CLI TRACE ",
file,
":",
fmt.Sprintf("%v", line),
" ",
fmt.Sprintf("(%s)", cf.Name()),
" ",
format,
}, ""),
a...,
)
}

func readURI(uriString string) ([]byte, error) {
u, err := url.Parse(uriString)
if err != nil {
return nil, err
}

if u.Host != "" { // i have a host, now do i support the scheme?
switch u.Scheme {
case "http", "https":
res, err := http.Get(uriString)
if err != nil {
return nil, err
}
return io.ReadAll(res.Body)
default:
return nil, fmt.Errorf("%[1]w: scheme of %[2]q is unsupported", Err, uriString)
}
} else if u.Path != "" ||
(runtime.GOOS == "windows" && strings.Contains(u.String(), "\\")) {
if _, notFoundFileErr := os.Stat(uriString); notFoundFileErr != nil {
return nil, fmt.Errorf("%[1]w: cannot read from %[2]q because it does not exist", Err, uriString)
}
return os.ReadFile(uriString)
}

return nil, fmt.Errorf("%[1]w: unable to determine how to load from %[2]q", Err, uriString)
}

// nestedVal checks if the name has '.' delimiters.
// If so, it tries to traverse the tree by the '.' delimited sections to find
// a nested value for the key.
func nestedVal(name string, tree map[any]any) (any, bool) {
if sections := strings.Split(name, "."); len(sections) > 1 {
node := tree
for _, section := range sections[:len(sections)-1] {
child, ok := node[section]
if !ok {
return nil, false
}

switch child := child.(type) {
case map[string]any:
node = make(map[any]any, len(child))
for k, v := range child {
node[k] = v
}
case map[any]any:
node = child
default:
return nil, false
}
}
if val, ok := node[sections[len(sections)-1]]; ok {
return val, true
}
}

return nil, false
}
17 changes: 17 additions & 0 deletions altsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package altsrc

import (
"context"
"time"

"github.com/urfave/cli-altsrc/v3/internal"
)

var (
testdataDir = func() string {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

return internal.MustTestdataDir(ctx)
}()
)
6 changes: 0 additions & 6 deletions default_input_source.go

This file was deleted.

Loading

0 comments on commit 26109e3

Please sign in to comment.