Skip to content

Commit

Permalink
chore: use dot as path default rather than absolute path (#1184)
Browse files Browse the repository at this point in the history
* use '.' in flags to indicate default function path

* regen commands.txt with '.' as default path

* centralize dot expansion

* minor cleanup

* update all references to path flag

* exits should be panics
  • Loading branch information
lkingland authored Aug 24, 2022
1 parent e465348 commit fecbc4e
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 88 deletions.
4 changes: 2 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro

// Check if the function has been initialized
if !function.Initialized() {
return fmt.Errorf("the given path '%v' does not contain an initialized function. Please create one at this path before deploying", config.Path)
return fmt.Errorf("'%v' does not contain an initialized function", config.Path)
}

// If a registry name was provided as a command line flag, it should be validated
Expand Down Expand Up @@ -234,7 +234,7 @@ type buildConfig struct {
func newBuildConfig() buildConfig {
return buildConfig{
Image: viper.GetString("image"),
Path: viper.GetString("path"),
Path: getPathFlag(),
Registry: viper.GetString("registry"),
Verbose: viper.GetBool("verbose"), // defined on root
Confirm: viper.GetBool("confirm"),
Expand Down
5 changes: 2 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/ory/viper"
"github.com/spf13/cobra"

fn "knative.dev/kn-plugin-func"
Expand Down Expand Up @@ -153,8 +152,8 @@ func newConfigCmdConfig(args []string) configCmdConfig {
name = args[0]
}
return configCmdConfig{
Name: deriveName(name, viper.GetString("path")),
Path: viper.GetString("path"),
Name: deriveName(name, getPathFlag()),
Path: getPathFlag(),
}

}
Expand Down
6 changes: 3 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func newDeleteConfig(args []string) deleteConfig {
name = args[0]
}
return deleteConfig{
Path: viper.GetString("path"),
Path: getPathFlag(),
Namespace: viper.GetString("namespace"),
DeleteAll: viper.GetBool("all"),
Name: deriveName(name, viper.GetString("path")), // args[0] or derived
Verbose: viper.GetBool("verbose"), // defined on root
Name: deriveName(name, getPathFlag()), // args[0] or derived
Verbose: viper.GetBool("verbose"), // defined on root
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func newDeployConfig(cmd *cobra.Command) (deployConfig, error) {
return deployConfig{
buildConfig: newBuildConfig(),
Namespace: viper.GetString("namespace"),
Path: viper.GetString("path"),
Path: getPathFlag(),
Verbose: viper.GetBool("verbose"), // defined on root
Confirm: viper.GetBool("confirm"),
BuildType: buildType,
Expand Down
4 changes: 1 addition & 3 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"io"
"os"
)

type Format string
Expand Down Expand Up @@ -48,7 +47,6 @@ func write(out io.Writer, s Formatter, formatName string) {
err = fmt.Errorf("format not recognized: %v\n", formatName)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
panic(err)
}
}
4 changes: 2 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func newInfoConfig(args []string) infoConfig {
name = args[0]
}
return infoConfig{
Name: deriveName(name, viper.GetString("path")),
Name: deriveName(name, getPathFlag()),
Namespace: viper.GetString("namespace"),
Output: viper.GetString("output"),
Path: viper.GetString("path"),
Path: getPathFlag(),
Verbose: viper.GetBool("verbose"),
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ EXAMPLES
}

// Flags
cmd.Flags().StringP("path", "p", cwd(), "Path to the function which should have its instance invoked. (Env: $FUNC_PATH)")
setPathFlag(cmd)
cmd.Flags().StringP("format", "f", "", "Format of message to send, 'http' or 'cloudevent'. Default is to choose automatically. (Env: $FUNC_FORMAT)")
cmd.Flags().StringP("target", "t", "", "Function instance to invoke. Can be 'local', 'remote' or a URL. Defaults to auto-discovery if not provided. (Env: $FUNC_TARGET)")
cmd.Flags().StringP("id", "", "", "ID for the request data. (Env: $FUNC_ID)")
Expand Down Expand Up @@ -205,7 +205,7 @@ type invokeConfig struct {

func newInvokeConfig(newClient ClientFactory) (cfg invokeConfig, err error) {
cfg = invokeConfig{
Path: viper.GetString("path"),
Path: getPathFlag(),
Target: viper.GetString("target"),
Format: viper.GetString("format"),
ID: viper.GetString("id"),
Expand Down
32 changes: 21 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ func interactiveTerminal() bool {
return err == nil && ((fi.Mode() & os.ModeCharDevice) != 0)
}

// cwd returns the current working directory or exits 1 printing the error.
func cwd() (cwd string) {
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to determine current working directory: %v", err)
os.Exit(1)
}
return cwd
}

// bindFunc which conforms to the cobra PreRunE method signature
type bindFunc func(*cobra.Command, []string) error

Expand Down Expand Up @@ -335,7 +325,27 @@ func mergeEnvs(envs []fn.Env, envToUpdate *util.OrderedMap, envToRemove []string

// setPathFlag ensures common text/wording when the --path flag is used
func setPathFlag(cmd *cobra.Command) {
cmd.Flags().StringP("path", "p", cwd(), "Path to the project directory (Env: $FUNC_PATH)")
cmd.Flags().StringP("path", "p", ".", "Path to the project directory (Env: $FUNC_PATH)")
}

// getPathFlag returns the value of the --path flag.
// The special value '.' is returned as the abolute path to the current
// working directory.
func getPathFlag() string {
path := viper.GetString("path")
if path == "." {
path = cwd()
}
return path
}

// cwd returns the current working directory or exits 1 printing the error.
func cwd() (cwd string) {
cwd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("Unable to determine current working directory: %v", err))
}
return cwd
}

type Version struct {
Expand Down
10 changes: 5 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ type runConfig struct {
Registry string
}

func newRunConfig(cmd *cobra.Command) (c runConfig, err error) {
func newRunConfig(cmd *cobra.Command) (cfg runConfig, err error) {
envToUpdate, envToRemove, err := envFromCmd(cmd)
if err != nil {
return
}

return runConfig{
cfg = runConfig{
Build: viper.GetString("build"),
Path: viper.GetString("path"),
Path: getPathFlag(),
Verbose: viper.GetBool("verbose"), // defined on root
Registry: viper.GetString("registry"),
EnvToUpdate: envToUpdate,
EnvToRemove: envToRemove,
}, nil
}
return
}
Loading

0 comments on commit fecbc4e

Please sign in to comment.