|
| 1 | +// Copyright Nitric Pty Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at: |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + tea "github.com/charmbracelet/bubbletea" |
| 23 | + "github.com/spf13/afero" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "github.com/nitrictech/cli/pkg/view/tui" |
| 27 | + add_website "github.com/nitrictech/cli/pkg/view/tui/commands/website" |
| 28 | + "github.com/nitrictech/cli/pkg/view/tui/teax" |
| 29 | +) |
| 30 | + |
| 31 | +// addCmd acts as a parent command for adding different types of resources |
| 32 | +// e.g., websites or other components in the future. |
| 33 | +var addCmd = &cobra.Command{ |
| 34 | + Use: "add", |
| 35 | + Short: "Add new resources to your Nitric project", |
| 36 | + Long: `Add new components such as websites to an existing Nitric project. |
| 37 | +Run 'nitric add website' to add a new website.`, |
| 38 | + Example: `# Add a new website interactively |
| 39 | +nitric add website`, |
| 40 | +} |
| 41 | + |
| 42 | +var addWebsiteCmd = &cobra.Command{ |
| 43 | + Use: "website [websiteName] [toolName]", |
| 44 | + Short: "Add a new website to your Nitric project", |
| 45 | + Long: `Add a new website to your Nitric project, with optional tool selection.`, |
| 46 | + Example: `# Interactive website addition |
| 47 | +nitric add website |
| 48 | +
|
| 49 | +# Add a new website with a specific tool |
| 50 | +nitric add website my-site astro`, |
| 51 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 52 | + fs := afero.NewOsFs() |
| 53 | + |
| 54 | + websiteName := "" |
| 55 | + if len(args) >= 1 { |
| 56 | + websiteName = args[0] |
| 57 | + } |
| 58 | + |
| 59 | + toolName := "" |
| 60 | + if len(args) >= 2 { |
| 61 | + toolName = args[1] |
| 62 | + } |
| 63 | + |
| 64 | + if !tui.IsTerminal() { |
| 65 | + return fmt.Errorf("non-interactive mode is not supported by this command") |
| 66 | + } |
| 67 | + |
| 68 | + websitePath, err := cmd.Flags().GetString("path") |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to get path flag: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + websiteModel, err := add_website.New(fs, add_website.Args{ |
| 74 | + WebsiteName: websiteName, |
| 75 | + ToolName: toolName, |
| 76 | + WebsitePath: websitePath, |
| 77 | + }) |
| 78 | + tui.CheckErr(err) |
| 79 | + |
| 80 | + if _, err := teax.NewProgram(websiteModel, tea.WithANSICompressor()).Run(); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | + }, |
| 86 | + Args: cobra.MaximumNArgs(2), |
| 87 | +} |
| 88 | + |
| 89 | +func init() { |
| 90 | + rootCmd.AddCommand(addCmd) |
| 91 | + |
| 92 | + addCmd.AddCommand(addWebsiteCmd) |
| 93 | + |
| 94 | + addStackCmd := &cobra.Command{ |
| 95 | + Use: "stack [stackName] [providerName]", |
| 96 | + Short: newStackCmd.Short, |
| 97 | + Long: newStackCmd.Long, |
| 98 | + RunE: newStackCmd.RunE, |
| 99 | + Args: newStackCmd.Args, |
| 100 | + } |
| 101 | + addStackCmd.Flags().AddFlagSet(newStackCmd.Flags()) |
| 102 | + addCmd.AddCommand(addStackCmd) |
| 103 | + |
| 104 | + addWebsiteCmd.Flags().StringP("path", "p", "", "base url path for the website, e.g. /my-site") |
| 105 | +} |
0 commit comments