Skip to content

Commit

Permalink
Allow for scheme-less strings (#3)
Browse files Browse the repository at this point in the history
* allow scheme-less URIs, block out app/ package

* Update to allow scheme-less strings; move guts of cmd/runtimevar in to /app

---------

Co-authored-by: sfomuseumbot <sfomuseumbot@localhost>
  • Loading branch information
thisisaaronland and sfomuseumbot authored Feb 22, 2024
1 parent 1f05295 commit 14c02d4
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 35 deletions.
15 changes: 15 additions & 0 deletions app/runtimevar/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package runtimevar

import (
"flag"

"github.com/sfomuseum/go-flags/flagset"
)

var timeout int

func DefaultFlagSet() *flag.FlagSet {
fs := flagset.NewFlagSet("publish")
fs.IntVar(&timeout, "timeout", 0, "The maximum number of second in which a variable can be resolved. If 0 no timeout is applied.")
return fs
}
34 changes: 34 additions & 0 deletions app/runtimevar/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package runtimevar

import (
"context"
"flag"
"fmt"

"github.com/sfomuseum/go-flags/flagset"
)

type RunOptions struct {
Timeout int
Vars []string
}

func OptionsFromFlagSet(ctx context.Context, fs *flag.FlagSet) (*RunOptions, error) {

flagset.Parse(fs)

err := flagset.SetFlagsFromEnvVars(fs, "RUNTIMEVAR")

if err != nil {
return nil, fmt.Errorf("Failed to derive flags from environment variables, %w", err)
}

vars := fs.Args()

opts := &RunOptions{
Timeout: timeout,
Vars: vars,
}

return opts, nil
}
51 changes: 51 additions & 0 deletions app/runtimevar/runtimetvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package runtimevar

import (
"context"
"flag"
"fmt"
"log/slog"
"time"

"github.com/sfomuseum/runtimevar"
)

func Run(ctx context.Context, logger *slog.Logger) error {
fs := DefaultFlagSet()
return RunWithFlagSet(ctx, fs, logger)
}

func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet, logger *slog.Logger) error {

opts, err := OptionsFromFlagSet(ctx, fs)

if err != nil {
return fmt.Errorf("Failed to derive options from flagset, %w", err)
}

return RunWithOptions(ctx, opts, logger)
}

func RunWithOptions(ctx context.Context, opts *RunOptions, logger *slog.Logger) error {

if opts.Timeout > 0 {

c, cancel := context.WithTimeout(ctx, time.Duration(opts.Timeout)*time.Second)
defer cancel()

ctx = c
}

for _, uri := range opts.Vars {

str_var, err := runtimevar.StringVar(ctx, uri)

if err != nil {
return fmt.Errorf("Failed to derive variable, %w", err)
}

fmt.Printf(str_var)
}

return nil
}
36 changes: 8 additions & 28 deletions cmd/runtimevar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,21 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"time"
"log/slog"
"os"

"github.com/sfomuseum/runtimevar"
_ "gocloud.dev/runtimevar/awsparamstore"
_ "gocloud.dev/runtimevar/constantvar"
_ "gocloud.dev/runtimevar/filevar"
"github.com/sfomuseum/runtimevar/app/runtimevar"
)

func main() {

timeout := flag.Int("timeout", 0, "The maximum number of second in which a variable can be resolved. If 0 no timeout is applied.")

flag.Parse()

ctx := context.Background()
logger := slog.Default()

if *timeout > 0 {

c, cancel := context.WithTimeout(ctx, time.Duration(*timeout)*time.Second)
defer cancel()

ctx = c
}

for _, uri := range flag.Args() {

str_var, err := runtimevar.StringVar(ctx, uri)

if err != nil {
log.Fatal(err)
}
err := runtimevar.Run(ctx, logger)

fmt.Printf(str_var)
if err != nil {
logger.Error("Failed to run application", "error", err)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/aaronland/go-aws-auth v1.3.1
github.com/sfomuseum/go-flags v0.10.0
gocloud.dev v0.36.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/sfomuseum/go-flags v0.10.0 h1:1OC1ACxpWMsl3XQ9OeNVMQj7Zi2CzufP3Rym3mPI8HU=
github.com/sfomuseum/go-flags v0.10.0/go.mod h1:VXOnnX1/yxQpX2yiwHaBV6aCmhtszQOL5bL1/nNo3co=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
18 changes: 11 additions & 7 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package runtimevar
import (
"context"
"fmt"
"github.com/aaronland/go-aws-auth"
"net/url"

"github.com/aaronland/go-aws-auth"
gc "gocloud.dev/runtimevar"
"gocloud.dev/runtimevar/awsparamstore"
_ "gocloud.dev/runtimevar/constantvar"
_ "gocloud.dev/runtimevar/filevar"
_ "log"
"net/url"
)

// StringVar returns the latest string value contained by 'uri', which is expected
Expand All @@ -19,7 +19,11 @@ func StringVar(ctx context.Context, uri string) (string, error) {
u, err := url.Parse(uri)

if err != nil {
return "", err
return "", fmt.Errorf("Failed to parse URI, %w", err)
}

if u.Scheme == "" {
return u.Path, nil
}

q := u.Query()
Expand Down Expand Up @@ -49,7 +53,7 @@ func StringVar(ctx context.Context, uri string) (string, error) {
return "", fmt.Errorf("Failed to create AWS session credentials, %w", err)
}

v, v_err = awsparamstore.OpenVariableV2(aws_auth, u.Host, gc.StringDecoder, nil)
v, v_err = awsparamstore.OpenVariableV2(aws_auth, u.Host, gc.StringDecoder, nil)
}

default:
Expand All @@ -64,15 +68,15 @@ func StringVar(ctx context.Context, uri string) (string, error) {
}

if v_err != nil {
return "", v_err
return "", fmt.Errorf("Failed to open variable, %w", v_err)
}

defer v.Close()

snapshot, err := v.Latest(ctx)

if err != nil {
return "", err
return "", fmt.Errorf("Failed to derive latest snapshot for variable, %w", err)
}

return snapshot.Value.(string), nil
Expand Down
2 changes: 2 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func TestConstantVar(t *testing.T) {

tests := map[string]string{
"constant://?val=hello+world": "hello world",
"/foo/bar/baz": "/foo/bar/baz",
"test": "test",
}

for uri, expected := range tests {
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/sfomuseum/go-flags/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions vendor/github.com/sfomuseum/go-flags/flagset/flagset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ github.com/googleapis/gax-go/v2/internal
# github.com/jmespath/go-jmespath v0.4.0
## explicit; go 1.14
github.com/jmespath/go-jmespath
# github.com/sfomuseum/go-flags v0.10.0
## explicit; go 1.16
github.com/sfomuseum/go-flags/flagset
# go.opencensus.io v0.24.0
## explicit; go 1.13
go.opencensus.io
Expand Down

0 comments on commit 14c02d4

Please sign in to comment.