|
| 1 | +/* |
| 2 | + * Copyright 2025 The CNAI 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 cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/spf13/viper" |
| 25 | + |
| 26 | + "github.com/CloudNativeAI/modctl/pkg/backend" |
| 27 | + "github.com/CloudNativeAI/modctl/pkg/config" |
| 28 | +) |
| 29 | + |
| 30 | +var uploadConfig = config.NewUpload() |
| 31 | + |
| 32 | +// uploadCmd represents the modctl command for upload. |
| 33 | +var uploadCmd = &cobra.Command{ |
| 34 | + Use: "upload [flags] <file>", |
| 35 | + Short: "A command line tool for modctl upload", |
| 36 | + Args: cobra.ExactArgs(1), |
| 37 | + DisableAutoGenTag: true, |
| 38 | + SilenceUsage: true, |
| 39 | + FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true}, |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + if err := uploadConfig.Validate(); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + return runUpload(context.Background(), args[0]) |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +// init initializes upload command. |
| 50 | +func init() { |
| 51 | + flags := uploadCmd.Flags() |
| 52 | + flags.StringVarP(&uploadConfig.Repo, "repo", "", "", "target model artifact repository name") |
| 53 | + flags.BoolVarP(&uploadConfig.PlainHTTP, "plain-http", "", false, "turning on this flag will use plain HTTP instead of HTTPS") |
| 54 | + flags.BoolVarP(&uploadConfig.Insecure, "insecure", "", false, "turning on this flag will disable TLS verification") |
| 55 | + flags.BoolVar(&uploadConfig.Raw, "raw", false, "turning on this flag will upload model artifact layer in raw format") |
| 56 | + |
| 57 | + if err := viper.BindPFlags(flags); err != nil { |
| 58 | + panic(fmt.Errorf("bind cache list flags to viper: %w", err)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// runUpload runs the upload modctl. |
| 63 | +func runUpload(ctx context.Context, filepath string) error { |
| 64 | + b, err := backend.New(rootConfig.StoargeDir) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + if err := b.Upload(ctx, filepath, uploadConfig); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + fmt.Printf("Successfully uploaded %s to model artifact repository: %s\n", filepath, uploadConfig.Repo) |
| 74 | + return nil |
| 75 | +} |
0 commit comments