| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// gocdk-runtimevar demonstrates the use of the Go CDK runtimevar package in a |
| 16 |
// simple command-line application. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"log" |
| 24 |
"os" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/google/subcommands" |
| 28 |
"gocloud.dev/runtimevar" |
| 29 |
|
| 30 |
// Import the runtimevar driver packages we want to be able to open. |
| 31 |
_ "gocloud.dev/runtimevar/awsparamstore" |
| 32 |
_ "gocloud.dev/runtimevar/blobvar" |
| 33 |
_ "gocloud.dev/runtimevar/constantvar" |
| 34 |
_ "gocloud.dev/runtimevar/filevar" |
| 35 |
_ "gocloud.dev/runtimevar/gcpruntimeconfig" |
| 36 |
_ "gocloud.dev/runtimevar/hashivault" |
| 37 |
_ "gocloud.dev/runtimevar/httpvar" |
| 38 |
) |
| 39 |
|
| 40 |
const helpSuffix = ` |
| 41 |
|
| 42 |
See https://gocloud.dev/concepts/urls/ for more background on |
| 43 |
Go CDK URLs, and sub-packages under gocloud.dev/runtimevar |
| 44 |
(https://godoc.org/gocloud.dev/runtimevar#pkg-subdirectories) |
| 45 |
for details on the runtimevar.Variable URL format. |
| 46 |
` |
| 47 |
|
| 48 |
func main() { |
| 49 |
os.Exit(run(context.Background())) |
| 50 |
} |
| 51 |
|
| 52 |
func run(ctx context.Context) int { |
| 53 |
subcommands.Register(subcommands.HelpCommand(), "") |
| 54 |
subcommands.Register(&catCmd{}, "") |
| 55 |
subcommands.Register(&watchCmd{}, "") |
| 56 |
log.SetFlags(0) |
| 57 |
log.SetPrefix("gocdk-runtimevar: ") |
| 58 |
flag.Parse() |
| 59 |
return int(subcommands.Execute(ctx)) |
| 60 |
} |
| 61 |
|
| 62 |
type catCmd struct{} |
| 63 |
|
| 64 |
func (*catCmd) Name() string { return "cat" } |
| 65 |
func (*catCmd) Synopsis() string { return "Print a variable's value to stdout" } |
| 66 |
func (*catCmd) Usage() string { |
| 67 |
return `cat <variable URL> |
| 68 |
|
| 69 |
Read the current value of the variable from <variable URL> and print it to stdout. |
| 70 |
|
| 71 |
Example: |
| 72 |
gocdk-runtimevar cat "constant://?val=foo&decoder=string"` + helpSuffix |
| 73 |
} |
| 74 |
|
| 75 |
func (*catCmd) SetFlags(_ *flag.FlagSet) {} |
| 76 |
|
| 77 |
func (*catCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 78 |
if f.NArg() != 1 { |
| 79 |
f.Usage() |
| 80 |
return subcommands.ExitUsageError |
| 81 |
} |
| 82 |
variableURL := f.Arg(0) |
| 83 |
|
| 84 |
// Open a *runtimevar.Variable using the variableURL. |
| 85 |
variable, err := runtimevar.OpenVariable(ctx, variableURL) |
| 86 |
if err != nil { |
| 87 |
log.Printf("Failed to open variable: %v\n", err) |
| 88 |
return subcommands.ExitFailure |
| 89 |
} |
| 90 |
defer variable.Close() |
| 91 |
|
| 92 |
snapshot, err := variable.Latest(ctx) |
| 93 |
if err != nil { |
| 94 |
log.Printf("Failed to read variable value: %v\n", err) |
| 95 |
return subcommands.ExitFailure |
| 96 |
} |
| 97 |
fmt.Printf("(%T) %v\n", snapshot.Value, snapshot.Value) |
| 98 |
return subcommands.ExitSuccess |
| 99 |
} |
| 100 |
|
| 101 |
type watchCmd struct{} |
| 102 |
|
| 103 |
func (*watchCmd) Name() string { return "watch" } |
| 104 |
func (*watchCmd) Synopsis() string { return "Watch a variable's value and print changes to stdout" } |
| 105 |
func (*watchCmd) Usage() string { |
| 106 |
return `watch <variable URL> |
| 107 |
|
| 108 |
Read the value of the variable from <variable URL> and print changes to stdout. |
| 109 |
|
| 110 |
Example: |
| 111 |
gocdk-runtimevar watch "constant://?val=foo&decoder=string"` + helpSuffix |
| 112 |
} |
| 113 |
|
| 114 |
func (*watchCmd) SetFlags(_ *flag.FlagSet) {} |
| 115 |
|
| 116 |
func (*watchCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 117 |
if f.NArg() != 1 { |
| 118 |
f.Usage() |
| 119 |
return subcommands.ExitUsageError |
| 120 |
} |
| 121 |
variableURL := f.Arg(0) |
| 122 |
|
| 123 |
// Open a *runtimevar.Variable using the variableURL. |
| 124 |
variable, err := runtimevar.OpenVariable(ctx, variableURL) |
| 125 |
if err != nil { |
| 126 |
log.Printf("Failed to open variable: %v\n", err) |
| 127 |
return subcommands.ExitFailure |
| 128 |
} |
| 129 |
defer variable.Close() |
| 130 |
|
| 131 |
fmt.Fprintf(os.Stderr, "Watching %s for changes...\n\n", variableURL) |
| 132 |
time.Sleep(250 * time.Millisecond) // to ensure deterministic combined output for test |
| 133 |
for { |
| 134 |
snapshot, err := variable.Watch(ctx) |
| 135 |
if err != nil { |
| 136 |
if err == context.Canceled { |
| 137 |
return subcommands.ExitSuccess |
| 138 |
} |
| 139 |
fmt.Printf("(error) %v\n", err) |
| 140 |
continue |
| 141 |
} |
| 142 |
fmt.Printf("(%T) %[1]v\n", snapshot.Value) |
| 143 |
} |
| 144 |
return subcommands.ExitSuccess |
| 145 |
} |