|
| 1 | +/* |
| 2 | + * Copyright 2021 The Java Operator SDK Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/java-operator-sdk/kubebuilder-plugin/pkg/quarkus/v1/scaffolds" |
| 26 | + "github.com/spf13/pflag" |
| 27 | + "k8s.io/apimachinery/pkg/util/validation" |
| 28 | + |
| 29 | + "sigs.k8s.io/kubebuilder/v3/pkg/config" |
| 30 | + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" |
| 31 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugin" |
| 32 | +) |
| 33 | + |
| 34 | +// This file represents the CLI for this plugin. |
| 35 | + |
| 36 | +const ( |
| 37 | + groupFlag = "group" |
| 38 | + versionFlag = "version" |
| 39 | + kindFlag = "kind" |
| 40 | +) |
| 41 | + |
| 42 | +type initSubcommand struct { |
| 43 | + apiSubcommand createAPISubcommand |
| 44 | + |
| 45 | + config config.Config |
| 46 | + |
| 47 | + // For help text. |
| 48 | + commandName string |
| 49 | + |
| 50 | + // Flags |
| 51 | + group string |
| 52 | + domain string |
| 53 | + version string |
| 54 | + kind string |
| 55 | + projectName string |
| 56 | +} |
| 57 | + |
| 58 | +var ( |
| 59 | + _ plugin.InitSubcommand = &initSubcommand{} |
| 60 | +) |
| 61 | + |
| 62 | +func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) { |
| 63 | + subcmdMeta.Description = `Initialize a new project based on the java-operator-sdk project. |
| 64 | +
|
| 65 | +Writes the following files: |
| 66 | +- a basic, Quarkus-based operator set-up |
| 67 | +- a pom.xml file to build the project with Maven |
| 68 | +` |
| 69 | + p.commandName = cliMeta.CommandName |
| 70 | +} |
| 71 | + |
| 72 | +func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) { |
| 73 | + //// TODO: include flags required for this plugin |
| 74 | + |
| 75 | + fs.SortFlags = false |
| 76 | + fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups") |
| 77 | + fs.StringVar(&p.projectName, "project-name", "", "name of this project, the default being directory name") |
| 78 | + |
| 79 | + fs.StringVar(&p.group, groupFlag, "", "resource Group") |
| 80 | + fs.StringVar(&p.version, versionFlag, "", "resource Version") |
| 81 | + fs.StringVar(&p.kind, kindFlag, "", "resource Kind") |
| 82 | + p.apiSubcommand.BindFlags(fs) |
| 83 | +} |
| 84 | + |
| 85 | +func (p *initSubcommand) InjectConfig(c config.Config) error { |
| 86 | + p.config = c |
| 87 | + |
| 88 | + if err := p.config.SetDomain(p.domain); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + // Assign a default project name |
| 93 | + if p.projectName == "" { |
| 94 | + dir, err := os.Getwd() |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("error getting current directory: %v", err) |
| 97 | + } |
| 98 | + p.projectName = strings.ToLower(filepath.Base(dir)) |
| 99 | + } |
| 100 | + // Check if the project name is a valid k8s namespace (DNS 1123 label). |
| 101 | + if err := validation.IsDNS1123Label(p.projectName); err != nil { |
| 102 | + return fmt.Errorf("project name (%s) is invalid: %v", p.projectName, err) |
| 103 | + } |
| 104 | + if err := p.config.SetProjectName(p.projectName); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func (p *initSubcommand) Validate() error { |
| 112 | + // TODO: validate the conditions you expect before running the plugin |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (p *initSubcommand) PostScaffold() error { |
| 117 | + // TODO: add anything you want to do AFTER the scaffolding has happened. |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error { |
| 122 | + scaffolder := scaffolds.NewInitScaffolder(p.config) |
| 123 | + scaffolder.InjectFS(fs) |
| 124 | + return scaffolder.Scaffold() |
| 125 | +} |
0 commit comments