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