|
| 1 | +/* |
| 2 | +Copyright © 2022 Meroxa Inc |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package apps |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli" |
| 22 | + |
| 23 | + "github.com/meroxa/cli/cmd/meroxa/builder" |
| 24 | + "github.com/meroxa/cli/log" |
| 25 | + "github.com/meroxa/meroxa-go/pkg/meroxa" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + _ builder.CommandWithDocs = (*Create)(nil) |
| 30 | + _ builder.CommandWithArgs = (*Create)(nil) |
| 31 | + _ builder.CommandWithFlags = (*Create)(nil) |
| 32 | + _ builder.CommandWithClient = (*Create)(nil) |
| 33 | + _ builder.CommandWithLogger = (*Create)(nil) |
| 34 | + _ builder.CommandWithExecute = (*Create)(nil) |
| 35 | +) |
| 36 | + |
| 37 | +type createApplicationClient interface { |
| 38 | + CreateApplication(ctx context.Context, input *meroxa.CreateApplicationInput) (*meroxa.Application, error) |
| 39 | +} |
| 40 | + |
| 41 | +type Create struct { |
| 42 | + client createApplicationClient |
| 43 | + logger log.Logger |
| 44 | + |
| 45 | + args struct { |
| 46 | + Name string |
| 47 | + } |
| 48 | + |
| 49 | + flags struct { |
| 50 | + Path string `long:"path" usage:"path where app was initialized to read its language from app.json (required unless specifying --lang)"` |
| 51 | + Lang string `long:"lang" short:"l" usage:"language to use (required unless specifying --path)"` |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (c *Create) Logger(logger log.Logger) { |
| 56 | + c.logger = logger |
| 57 | +} |
| 58 | + |
| 59 | +func (c *Create) Client(client meroxa.Client) { |
| 60 | + c.client = client |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Create) Flags() []builder.Flag { |
| 64 | + return builder.BuildFlags(&c.flags) |
| 65 | +} |
| 66 | + |
| 67 | +func (c *Create) ParseArgs(args []string) error { |
| 68 | + if len(args) < 1 { |
| 69 | + return errors.New("requires an application name") |
| 70 | + } |
| 71 | + c.args.Name = args[0] |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (c *Create) getLang(ctx context.Context) (string, error) { |
| 76 | + if path := c.flags.Path; path != "" { |
| 77 | + lang, err := turbineCLI.GetLangFromAppJSON(path) |
| 78 | + if err != nil { |
| 79 | + return lang, err |
| 80 | + } |
| 81 | + if c.flags.Lang != "" && c.flags.Lang != lang { |
| 82 | + c.logger.Info(ctx, "Ignoring language flag.") |
| 83 | + } |
| 84 | + return lang, nil |
| 85 | + } |
| 86 | + |
| 87 | + return c.flags.Lang, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (c *Create) Execute(ctx context.Context) error { |
| 91 | + if c.flags.Lang == "" && c.flags.Path == "" { |
| 92 | + return fmt.Errorf("language is required either using --path ~/turbine/my-app or --lang. Type `meroxa help apps create` for more information") //nolint:lll |
| 93 | + } |
| 94 | + |
| 95 | + lang, err := c.getLang(ctx) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + input := meroxa.CreateApplicationInput{ |
| 101 | + Name: c.args.Name, |
| 102 | + Language: lang, |
| 103 | + } |
| 104 | + |
| 105 | + c.logger.Infof(ctx, "Creating application %q with language %q...", input.Name, lang) |
| 106 | + |
| 107 | + res, err := c.client.CreateApplication(ctx, &input) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + c.logger.Infof(ctx, "Application %q successfully created!", res.Name) |
| 113 | + c.logger.JSON(ctx, res) |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func (c *Create) Usage() string { |
| 119 | + return "create NAME" |
| 120 | +} |
| 121 | + |
| 122 | +func (c *Create) Docs() builder.Docs { |
| 123 | + return builder.Docs{ |
| 124 | + Short: "Create a Meroxa Data Application", |
| 125 | + Long: "You'll be able to use this application for consequent build via `meroxa apps deploy`. You'll need to specify " + |
| 126 | + "language used either via `--lang` or specifying with `--path` the location of your app.json which should contain the desired language.", |
| 127 | + Example: "meroxa apps create my-app --language golang\n" + |
| 128 | + "meroxa apps create my-app --path ~/turbine/my-app", |
| 129 | + } |
| 130 | +} |
0 commit comments