Skip to content

Commit

Permalink
feat(cli): add envd images remove command (#1007)
Browse files Browse the repository at this point in the history
Signed-off-by: Triple-Z <me@triplez.cn>

Signed-off-by: Triple-Z <me@triplez.cn>
  • Loading branch information
Triple-Z committed Oct 13, 2022
1 parent f26fc4c commit 931e1a0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/app/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var CommandImage = &cli.Command{
Subcommands: []*cli.Command{
CommandDescribeImage,
CommandListImage,
CommandRemoveImage,
},
}

Expand Down
68 changes: 68 additions & 0 deletions pkg/app/image_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"fmt"

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/docker"
)

var CommandRemoveImage = &cli.Command{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove an envd image",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "image",
Usage: "Specify the image name to be removed",
Aliases: []string{"i"},
},
&cli.StringFlag{
Name: "tag",
Usage: "Remove the image with a specific tag",
Aliases: []string{"t"},
DefaultText: "dev",
},
},
Action: removeImage,
}

func removeImage(clicontext *cli.Context) error {
imageName := clicontext.String("image")
if imageName == "" {
return errors.New("image name is required, find images by `envd images list`")
}
tag := clicontext.String("tag")
if tag == "" {
logrus.Debug("tag not specified, using default tag: `dev`")
tag = "dev"
}
imageNameWithTag := fmt.Sprintf("%s:%s", imageName, tag)

dockerClient, err := docker.NewClient(clicontext.Context)
if err != nil {
return err
}
if err := dockerClient.RemoveImage(clicontext.Context, imageNameWithTag); err != nil {
return errors.Errorf("remove image %s failed: %w", imageNameWithTag, err)
}
logrus.Infof("image(%s) has removed", imageNameWithTag)
return nil
}

0 comments on commit 931e1a0

Please sign in to comment.