|
| 1 | +// Copyright 2025 The envd Authors |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package app |
| 16 | + |
| 17 | +import ( |
| 18 | + _ "embed" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/cockroachdb/errors" |
| 25 | + "github.com/sirupsen/logrus" |
| 26 | + cli "github.com/urfave/cli/v2" |
| 27 | + |
| 28 | + "github.com/tensorchord/envd/pkg/util/fileutil" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + //go:embed template/uv.envd |
| 33 | + templateUV string |
| 34 | + //go:embed template/conda.envd |
| 35 | + templateConda string |
| 36 | + //go:embed template/torch.envd |
| 37 | + templateTorch string |
| 38 | + |
| 39 | + templates = map[string]string{ |
| 40 | + "uv": templateUV, |
| 41 | + "conda": templateConda, |
| 42 | + "torch": templateTorch, |
| 43 | + } |
| 44 | +) |
| 45 | + |
| 46 | +func joinKeysToString(table map[string]string) string { |
| 47 | + keys := make([]string, 0, len(table)) |
| 48 | + for k := range table { |
| 49 | + keys = append(keys, k) |
| 50 | + } |
| 51 | + return strings.Join(keys, ", ") |
| 52 | +} |
| 53 | + |
| 54 | +func isDefaultTemplate(name string) bool { |
| 55 | + _, ok := templates[name] |
| 56 | + return ok |
| 57 | +} |
| 58 | + |
| 59 | +var CommandNew = &cli.Command{ |
| 60 | + Name: "new", |
| 61 | + Category: CategoryBasic, |
| 62 | + Aliases: []string{"n"}, |
| 63 | + Usage: "Create a new `build.envd` file from pre-defined templates", |
| 64 | + Description: `The template used by this command is stored in the |
| 65 | + '$HOME/.config/envd/templates' directory, we provide some pre-defined templates for you |
| 66 | + to use during 'envd bootstrap', you can also add your own templates to this directory.`, |
| 67 | + Flags: []cli.Flag{ |
| 68 | + &cli.StringFlag{ |
| 69 | + Name: "template", |
| 70 | + Usage: fmt.Sprintf("Template name to use (`envd bootstrap` will add [%s])", joinKeysToString(templates)), |
| 71 | + Aliases: []string{"t"}, |
| 72 | + Required: true, |
| 73 | + }, |
| 74 | + &cli.BoolFlag{ |
| 75 | + Name: "force", |
| 76 | + Usage: "Overwrite the build.envd if existed", |
| 77 | + Aliases: []string{"f"}, |
| 78 | + Required: false, |
| 79 | + }, |
| 80 | + &cli.PathFlag{ |
| 81 | + Name: "path", |
| 82 | + Usage: "Path to the directory of the build.envd", |
| 83 | + Aliases: []string{"p"}, |
| 84 | + Value: ".", |
| 85 | + }, |
| 86 | + }, |
| 87 | + Action: newCommand, |
| 88 | +} |
| 89 | + |
| 90 | +func newCommand(clicontext *cli.Context) error { |
| 91 | + workDir, err := filepath.Abs(clicontext.Path("path")) |
| 92 | + if err != nil { |
| 93 | + return errors.Wrap(err, "failed to get absolute path") |
| 94 | + } |
| 95 | + |
| 96 | + force := clicontext.Bool("force") |
| 97 | + filePath := filepath.Join(workDir, "build.envd") |
| 98 | + exists, err := fileutil.FileExists(filePath) |
| 99 | + if err != nil { |
| 100 | + return errors.Wrap(err, "failed to check file exists") |
| 101 | + } |
| 102 | + if exists && !force { |
| 103 | + return errors.New("build.envd already exists, use `--force` to overwrite") |
| 104 | + } |
| 105 | + |
| 106 | + template := clicontext.String("template") |
| 107 | + templateFile := fmt.Sprintf("%s.envd", template) |
| 108 | + templatePath, err := fileutil.TemplateFile(templateFile) |
| 109 | + if err != nil { |
| 110 | + return errors.Wrapf(err, "failed to get template file: `%s`", templateFile) |
| 111 | + } |
| 112 | + |
| 113 | + content, err := os.ReadFile(templatePath) |
| 114 | + if err != nil { |
| 115 | + if os.IsNotExist(err) && isDefaultTemplate(template) { |
| 116 | + // Add default templates to the template directory if not exist |
| 117 | + err = addTemplates(clicontext) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + content, err = os.ReadFile(templatePath) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + } else { |
| 126 | + return errors.Wrapf(err, "failed to read the template file `%s`", templatePath) |
| 127 | + } |
| 128 | + } |
| 129 | + err = os.WriteFile(filePath, content, 0644) |
| 130 | + if err != nil { |
| 131 | + return errors.Wrapf(err, "failed to write the build.envd file") |
| 132 | + } |
| 133 | + logrus.Infof("Template `%s` is created in `%s`", template, filePath) |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func addTemplates(clicontext *cli.Context) error { |
| 139 | + for name, content := range templates { |
| 140 | + file, err := fileutil.TemplateFile(name + ".envd") |
| 141 | + if err != nil { |
| 142 | + return errors.Wrapf(err, "failed to get template file path: %s", name) |
| 143 | + } |
| 144 | + exist, err := fileutil.FileExists(file) |
| 145 | + if err != nil { |
| 146 | + return errors.Wrapf(err, "failed to check file exists: %s", file) |
| 147 | + } |
| 148 | + if exist { |
| 149 | + logrus.Debugf("Template file `%s` already exists in `%s`", name, file) |
| 150 | + continue |
| 151 | + } |
| 152 | + err = os.WriteFile(file, []byte(content), 0644) |
| 153 | + if err != nil { |
| 154 | + return errors.Wrapf(err, "failed to write template file: %s", name) |
| 155 | + } |
| 156 | + logrus.Debugf("Template file `%s` is added to `%s`", name, file) |
| 157 | + } |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
0 commit comments