Add -c/--config flag

Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
Andrew Keesler
2020-07-08 13:06:44 -04:00
parent 568febea79
commit 619ae2b178
5 changed files with 174 additions and 6 deletions

View File

@@ -0,0 +1,70 @@
/*
Copyright 2020 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
// Package app is the command line entry point for placeholder-name.
package app
import (
"io"
"log"
"net/http"
"github.com/spf13/cobra"
"github.com/suzerain-io/placeholder-name/pkg/handlers"
)
// App is an object that represents the placeholder-name application.
type App struct {
cmd *cobra.Command
// runFunc runs the actual program, after the parsing of flags has been done.
//
// It is mostly a field for the sake of testing.
runFunc func(configPath string)
}
// New constructs a new App with command line args, stdout and stderr.
func New(args []string, stdout, stderr io.Writer) *App {
a := &App{
runFunc: func(configPath string) {
addr := ":8080"
log.Printf("Starting server on %v", addr)
log.Fatal(http.ListenAndServe(addr, handlers.New()))
},
}
var configPath string
cmd := &cobra.Command{
Use: `placeholder-name`,
Long: `placeholder-name provides a generic API for mapping an external
credential from somewhere to an internal credential to be used for
authenticating to the Kubernetes API.`,
Run: func(cmd *cobra.Command, args []string) {
a.runFunc(configPath)
},
Args: cobra.NoArgs,
}
cmd.SetArgs(args)
cmd.SetOut(stdout)
cmd.SetErr(stderr)
cmd.Flags().StringVarP(
&configPath,
"config",
"c",
"placeholder-name.yaml",
"path to configuration file",
)
a.cmd = cmd
return a
}
func (a *App) Run() error {
return a.cmd.Execute()
}

View File

@@ -0,0 +1,84 @@
/*
Copyright 2020 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package app
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
const knownGoodUsage = `Usage:
placeholder-name [flags]
Flags:
-c, --config string path to configuration file (default "placeholder-name.yaml")
-h, --help help for placeholder-name
`
func TestCommand(t *testing.T) {
tests := []struct {
name string
args []string
wantConfigPath string
}{
{
name: "NoArgsSucceeds",
args: []string{},
wantConfigPath: "placeholder-name.yaml",
},
{
name: "OneArgFails",
args: []string{"tuna"},
},
{
name: "ShortConfigFlagSucceeds",
args: []string{"-c", "some/path/to/config.yaml"},
wantConfigPath: "some/path/to/config.yaml",
},
{
name: "LongConfigFlagSucceeds",
args: []string{"--config", "some/path/to/config.yaml"},
wantConfigPath: "some/path/to/config.yaml",
},
{
name: "OneArgWithConfigFlagFails",
args: []string{
"--config", "some/path/to/config.yaml",
"tuna",
},
},
}
for _, theTest := range tests {
test := theTest // please the linter :'(
t.Run(test.name, func(t *testing.T) {
expect := assert.New(t)
stdout := bytes.NewBuffer([]byte{})
stderr := bytes.NewBuffer([]byte{})
configPaths := make([]string, 0, 1)
runFunc := func(configPath string) {
configPaths = append(configPaths, configPath)
}
a := New(test.args, stdout, stderr)
a.runFunc = runFunc
err := a.Run()
if test.wantConfigPath != "" {
if expect.Equal(1, len(configPaths)) {
expect.Equal(test.wantConfigPath, configPaths[0])
}
} else {
expect.Error(err)
expect.Contains(stdout.String(), knownGoodUsage)
}
})
}
}

View File

@@ -6,14 +6,13 @@ SPDX-License-Identifier: Apache-2.0
package main
import (
"log"
"net/http"
"os"
"github.com/suzerain-io/placeholder-name/pkg/handlers"
"github.com/suzerain-io/placeholder-name/cmd/placeholder-name/app"
)
func main() {
addr := ":8080"
log.Printf("Starting server on %v", addr)
log.Fatal(http.ListenAndServe(addr, handlers.New()))
if err := app.New(os.Args[1:], os.Stdout, os.Stderr).Run(); err != nil {
os.Exit(1)
}
}