mirror of https://github.com/knative/func.git
Refactor if-elif-else => switch (#2785)
Signed-off-by: Matej Vašek <mvasek@redhat.com>
This commit is contained in:
parent
0896b9011d
commit
f12acd34de
|
@ -383,22 +383,23 @@ func (c buildConfig) Validate() (err error) {
|
|||
// deployment is not the contiainer, but rather the running service.
|
||||
func (c buildConfig) clientOptions() ([]fn.Option, error) {
|
||||
o := []fn.Option{fn.WithRegistry(c.Registry)}
|
||||
if c.Builder == builders.Host {
|
||||
switch c.Builder {
|
||||
case builders.Host:
|
||||
o = append(o,
|
||||
fn.WithBuilder(oci.NewBuilder(builders.Host, c.Verbose)),
|
||||
fn.WithPusher(oci.NewPusher(c.RegistryInsecure, false, c.Verbose)))
|
||||
} else if c.Builder == builders.Pack {
|
||||
case builders.Pack:
|
||||
o = append(o,
|
||||
fn.WithBuilder(pack.NewBuilder(
|
||||
pack.WithName(builders.Pack),
|
||||
pack.WithTimestamp(c.WithTimestamp),
|
||||
pack.WithVerbose(c.Verbose))))
|
||||
} else if c.Builder == builders.S2I {
|
||||
case builders.S2I:
|
||||
o = append(o,
|
||||
fn.WithBuilder(s2i.NewBuilder(
|
||||
s2i.WithName(builders.S2I),
|
||||
s2i.WithVerbose(c.Verbose))))
|
||||
} else {
|
||||
default:
|
||||
return o, builders.ErrUnknownBuilder{Name: c.Builder, Known: KnownBuilders()}
|
||||
}
|
||||
return o, nil
|
||||
|
|
|
@ -111,33 +111,36 @@ func runConfigCmd(cmd *cobra.Command, args []string) (err error) {
|
|||
|
||||
switch answers.SelectedOperation {
|
||||
case "Add":
|
||||
if answers.SelectedConfig == "Volumes" {
|
||||
switch answers.SelectedConfig {
|
||||
case "Volumes":
|
||||
err = runAddVolumesPrompt(cmd.Context(), function)
|
||||
} else if answers.SelectedConfig == "Environment variables" {
|
||||
case "Environment variables":
|
||||
err = runAddEnvsPrompt(cmd.Context(), function)
|
||||
} else if answers.SelectedConfig == "Labels" {
|
||||
case "Labels":
|
||||
err = runAddLabelsPrompt(cmd.Context(), function, defaultLoaderSaver)
|
||||
} else if answers.SelectedConfig == "Git" {
|
||||
case "Git":
|
||||
err = runConfigGitSetCmd(cmd, NewClient)
|
||||
}
|
||||
case "Remove":
|
||||
if answers.SelectedConfig == "Volumes" {
|
||||
switch answers.SelectedConfig {
|
||||
case "Volumes":
|
||||
err = runRemoveVolumesPrompt(function)
|
||||
} else if answers.SelectedConfig == "Environment variables" {
|
||||
case "Environment variables":
|
||||
err = runRemoveEnvsPrompt(function)
|
||||
} else if answers.SelectedConfig == "Labels" {
|
||||
case "Labels":
|
||||
err = runRemoveLabelsPrompt(function, defaultLoaderSaver)
|
||||
} else if answers.SelectedConfig == "Git" {
|
||||
case "Git":
|
||||
err = runConfigGitRemoveCmd(cmd, NewClient)
|
||||
}
|
||||
case "List":
|
||||
if answers.SelectedConfig == "Volumes" {
|
||||
switch answers.SelectedConfig {
|
||||
case "Volumes":
|
||||
listVolumes(function)
|
||||
} else if answers.SelectedConfig == "Environment variables" {
|
||||
case "Environment variables":
|
||||
err = listEnvs(function, cmd.OutOrStdout(), Human)
|
||||
} else if answers.SelectedConfig == "Labels" {
|
||||
case "Labels":
|
||||
listLabels(function)
|
||||
} else if answers.SelectedConfig == "Git" {
|
||||
case "Git":
|
||||
err = runConfigGitCmd(cmd, NewClient)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -218,8 +218,8 @@ func handleSession(t *testing.T, newChannel ssh.NewChannel) {
|
|||
t.Error(err)
|
||||
}
|
||||
var ret uint32
|
||||
switch {
|
||||
case data.Command == "set":
|
||||
switch data.Command {
|
||||
case "set":
|
||||
ret = 0
|
||||
_, _ = fmt.Fprintf(ch, "DOCKER_HOST=unix://%s\n", sshDockerSocket)
|
||||
default:
|
||||
|
|
|
@ -802,10 +802,12 @@ func (c *Client) Deploy(ctx context.Context, f Function, oo ...DeployOption) (Fu
|
|||
// Update the function to reflect the new deployed state of the Function
|
||||
f.Deploy.Namespace = result.Namespace
|
||||
|
||||
if result.Status == Deployed {
|
||||
switch result.Status {
|
||||
case Deployed:
|
||||
fmt.Fprintf(os.Stderr, "✅ Function deployed in namespace %q and exposed at URL: \n %v\n", result.Namespace, result.URL)
|
||||
} else if result.Status == Updated {
|
||||
case Updated:
|
||||
fmt.Fprintf(os.Stderr, "✅ Function updated in namespace %q and exposed at URL: \n %v\n", result.Namespace, result.URL)
|
||||
default:
|
||||
}
|
||||
|
||||
return f, nil
|
||||
|
|
|
@ -104,7 +104,8 @@ func invoke(ctx context.Context, c *Client, f Function, target string, m InvokeM
|
|||
// errors if neither are available.
|
||||
func invocationRoute(ctx context.Context, c *Client, f Function, target string) (string, error) {
|
||||
// TODO: this function has code-smell; will de-smellify it in next pass.
|
||||
if target == EnvironmentLocal {
|
||||
switch target {
|
||||
case EnvironmentLocal:
|
||||
instance, err := c.Instances().Get(ctx, f, target)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEnvironmentNotFound) {
|
||||
|
@ -113,8 +114,7 @@ func invocationRoute(ctx context.Context, c *Client, f Function, target string)
|
|||
return "", err
|
||||
}
|
||||
return instance.Route, nil
|
||||
|
||||
} else if target == EnvironmentRemote {
|
||||
case EnvironmentRemote:
|
||||
instance, err := c.Instances().Get(ctx, f, target)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEnvironmentNotFound) {
|
||||
|
@ -123,8 +123,7 @@ func invocationRoute(ctx context.Context, c *Client, f Function, target string)
|
|||
return "", err
|
||||
}
|
||||
return instance.Route, nil
|
||||
|
||||
} else if target == "" { // target blank, check local first then remote.
|
||||
case "":
|
||||
instance, err := c.Instances().Get(ctx, f, EnvironmentLocal)
|
||||
if err != nil && !errors.Is(err, ErrNotRunning) {
|
||||
return "", err // unexpected errors are anything other than ErrNotRunning
|
||||
|
@ -140,7 +139,7 @@ func invocationRoute(ctx context.Context, c *Client, f Function, target string)
|
|||
return "", err // unexpected error
|
||||
}
|
||||
return instance.Route, nil
|
||||
} else { // treat an unrecognized target as an ad-hoc verbatim endpoint
|
||||
default:
|
||||
return target, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,11 +139,12 @@ func createPipelineTemplatePAC(f fn.Function, labels map[string]string) error {
|
|||
}
|
||||
|
||||
var template string
|
||||
if f.Build.Builder == builders.Pack {
|
||||
switch f.Build.Builder {
|
||||
case builders.Pack:
|
||||
template = packPipelineTemplate
|
||||
} else if f.Build.Builder == builders.S2I {
|
||||
case builders.S2I:
|
||||
template = s2iPipelineTemplate
|
||||
} else {
|
||||
default:
|
||||
return builders.ErrBuilderNotSupported{Builder: f.Build.Builder}
|
||||
}
|
||||
|
||||
|
@ -212,11 +213,12 @@ func createPipelineRunTemplatePAC(f fn.Function, labels map[string]string) error
|
|||
}
|
||||
|
||||
var template string
|
||||
if f.Build.Builder == builders.Pack {
|
||||
switch f.Build.Builder {
|
||||
case builders.Pack:
|
||||
template = packRunTemplatePAC
|
||||
} else if f.Build.Builder == builders.S2I {
|
||||
case builders.S2I:
|
||||
template = s2iRunTemplatePAC
|
||||
} else {
|
||||
default:
|
||||
return builders.ErrBuilderNotSupported{Builder: f.Build.Builder}
|
||||
}
|
||||
|
||||
|
@ -339,11 +341,12 @@ func createAndApplyPipelineTemplate(f fn.Function, namespace string, labels map[
|
|||
}
|
||||
|
||||
var template string
|
||||
if f.Build.Builder == builders.Pack {
|
||||
switch f.Build.Builder {
|
||||
case builders.Pack:
|
||||
template = packPipelineTemplate
|
||||
} else if f.Build.Builder == builders.S2I {
|
||||
case builders.S2I:
|
||||
template = s2iPipelineTemplate
|
||||
} else {
|
||||
default:
|
||||
return builders.ErrBuilderNotSupported{Builder: f.Build.Builder}
|
||||
}
|
||||
|
||||
|
@ -401,11 +404,12 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m
|
|||
}
|
||||
|
||||
var template string
|
||||
if f.Build.Builder == builders.Pack {
|
||||
switch f.Build.Builder {
|
||||
case builders.Pack:
|
||||
template = packRunTemplate
|
||||
} else if f.Build.Builder == builders.S2I {
|
||||
case builders.S2I:
|
||||
template = s2iRunTemplate
|
||||
} else {
|
||||
default:
|
||||
return builders.ErrBuilderNotSupported{Builder: f.Build.Builder}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,18 +25,18 @@ func (e ErrRuntimeNotSupported) Error() string {
|
|||
}
|
||||
|
||||
func validatePipeline(f fn.Function) error {
|
||||
if f.Build.Builder == builders.Pack {
|
||||
switch f.Build.Builder {
|
||||
case builders.Pack:
|
||||
if f.Runtime == "" {
|
||||
return ErrRuntimeRequired
|
||||
}
|
||||
|
||||
if len(f.Build.Buildpacks) > 0 {
|
||||
return ErrBuilpacksNotSupported
|
||||
}
|
||||
} else if f.Build.Builder == builders.S2I {
|
||||
case builders.S2I:
|
||||
_, err := s2i.BuilderImage(f, builders.S2I)
|
||||
return err
|
||||
} else {
|
||||
default:
|
||||
return builders.ErrUnknownBuilder{Name: f.Build.Builder}
|
||||
}
|
||||
|
||||
|
|
|
@ -72,12 +72,12 @@ func Extract(input io.Reader, destDir string) error {
|
|||
return fmt.Errorf("cannot ensure parent: %w", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case hdr.Typeflag == tar.TypeReg:
|
||||
switch hdr.Typeflag {
|
||||
case tar.TypeReg:
|
||||
err = writeRegularFile(destPath, os.FileMode(hdr.Mode&0777), r)
|
||||
case hdr.Typeflag == tar.TypeDir:
|
||||
case tar.TypeDir:
|
||||
err = os.MkdirAll(destPath, os.FileMode(hdr.Mode)&fs.ModePerm)
|
||||
case hdr.Typeflag == tar.TypeSymlink:
|
||||
case tar.TypeSymlink:
|
||||
err = os.Symlink(linkname, destPath)
|
||||
default:
|
||||
_, _ = fmt.Printf("unsupported type flag: %d\n", hdr.Typeflag)
|
||||
|
|
Loading…
Reference in New Issue