From b38027c3ccfb1523dac6212f3c08c98dffd5e7d7 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Tue, 12 Nov 2024 05:35:20 +0000 Subject: [PATCH] Verify docker image working on CI --- .github/workflows/test.yml | 2 ++ goreleaser.yml | 1 + main.go | 9 +++++++- parse.go | 42 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 parse.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8904ffd..e8b6d92 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -65,3 +65,5 @@ jobs: uses: goreleaser/goreleaser-action@v6 with: args: release --snapshot + - name: Verify docker images working + run: docker run --rm ghcr.io/chatwork/aws-checkercanary-amd64 aws-checker version diff --git a/goreleaser.yml b/goreleaser.yml index b9ae096..3cdd328 100644 --- a/goreleaser.yml +++ b/goreleaser.yml @@ -8,6 +8,7 @@ builds: - CGO_ENABLED=0 ldflags: - -s -w + - -X main.Version={{.Version}} goos: - darwin - linux diff --git a/main.go b/main.go index 448c6a2..0261083 100644 --- a/main.go +++ b/main.go @@ -19,13 +19,20 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) +var Version = "dev" + func main() { + opts, code := parseFlags(os.Args[1:]) + if code != nil { + os.Exit(*code) + } + // Create a channel to receive OS signals sigs := make(chan os.Signal, 1) // Register the channel to receive SIGINT, SIGTERM signals signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - if err := Run(ContextWithSignal(context.Background(), sigs)); err != nil { + if err := Run(ContextWithSignal(context.Background(), sigs), opts...); err != nil { log.Fatalf("Error: %v", err) } } diff --git a/parse.go b/parse.go new file mode 100644 index 0000000..717b0e0 --- /dev/null +++ b/parse.go @@ -0,0 +1,42 @@ +package main + +import ( + "flag" + "fmt" +) + +func parseFlags(args []string) ([]Option, *int) { + fs := flag.NewFlagSet("aws-checker", flag.ContinueOnError) + fs.Usage = func() { + fmt.Fprintf(fs.Output(), "%s is a toolkit for checking availability of AWS services.\n", fs.Name()) + fmt.Fprintf(fs.Output(), "Run '%s -help' for usage.\n", fs.Name()) + } + + var code int + + if err := fs.Parse(args); err != nil { + if err != flag.ErrHelp { + code = 2 + } + } else { + switch fs.NArg() { + case 0: + return []Option{}, nil + case 1: + switch fs.Arg(0) { + case "version": + fmt.Fprintf(fs.Output(), "%s %s", fs.Name(), Version) + default: + fmt.Fprintf(fs.Output(), "unknown command %q for %q\n", fs.Arg(0), fs.Name()) + fmt.Fprintf(fs.Output(), "Run '%s -help' for usage.\n", fs.Name()) + code = 2 + } + default: + fmt.Fprintf(fs.Output(), "too many arguments\n") + fmt.Fprintf(fs.Output(), "Run '%s -help' for usage.\n", fs.Name()) + code = 2 + } + } + + return nil, &code +}