Skip to content

Commit

Permalink
Stop コマンドの実装。
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoto2000 committed Jun 20, 2024
1 parent 92970d7 commit 57a9ea9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
58 changes: 58 additions & 0 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,64 @@ func ExecuteDevcontainer(args []string, devcontainerPath string, vimFilePath str
// コンテナ停止は別途 down コマンドで行う
}

func Stop(args []string, devcontainerPath string, configDirForDevcontainer string) {

// `devcontainer read-configuration` で docker compose の利用判定

// コマンドライン引数の末尾は `--workspace-folder` の値として使う
workspaceFolder := args[len(args)-1]
stdout, _ := ReadConfiguration(devcontainerPath, "--workspace-folder", workspaceFolder)
if stdout == "" {
fmt.Printf("This directory is not a workspace for devcontainer: %s\n", workspaceFolder)
os.Exit(0)
}

// `dockerComposeFile` が含まれているかを確認する
// 含まれているなら docker compose によるコンテナ構築がされている
if strings.Contains(stdout, "dockerComposeFile") {

// docker compose ps コマンドで compose の情報取得
dockerComposePsResultString, err := dockercompose.Ps(workspaceFolder)
if err != nil {
panic(err)
}
if dockerComposePsResultString == "" {
fmt.Println("devcontainer already downed.")
os.Exit(0)
}

// 必要なのは最初の 1 行だけなので、最初の 1 行のみを取得
dockerComposePsResultFirstItemString := strings.Split(dockerComposePsResultString, "\n")[0]

// docker compose ps コマンドの結果からプロジェクト名を取得
projectName, err := dockercompose.GetProjectName(dockerComposePsResultFirstItemString)
if err != nil {
panic(err)
}

// プロジェクト名を使って docker compose down を実行
fmt.Printf("Run `docker compose -p %s stop`(Async)\n", projectName)
err = dockercompose.Stop(projectName)
if err != nil {
panic(err)
}
} else {
// ワークスペースに対応するコンテナを探して ID を取得する
containerID, err := docker.GetContainerIDFromWorkspaceFolder(workspaceFolder)
if err != nil {
panic(err)
}

// 取得したコンテナに対して stop を行う
fmt.Printf("Run `docker stop -f %s stop`(Async)\n", containerID)
err = docker.Stop(containerID)
if err != nil {
panic(err)
}
}

}

func Down(args []string, devcontainerPath string, configDirForDevcontainer string) {

// `devcontainer read-configuration` で docker compose の利用判定
Expand Down
7 changes: 7 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func Ps(filter string) (string, error) {
return string(stdout), err
}

// `docker stop -f ${containerID}` コマンドを実行する。
func Stop(containerID string) error {
dockerStopCommand := exec.Command("docker", "stop", containerID)
err := dockerStopCommand.Start()
return err
}

// `docker rm -f ${containerID}` コマンドを実行する。
func Rm(containerID string) error {
dockerRmCommand := exec.Command("docker", "rm", "-f", containerID)
Expand Down
6 changes: 6 additions & 0 deletions dockercompose/dockerCompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func Ps(workspaceFolder string) (string, error) {
return string(stdout), err
}

// `docker compose -p ${projectName} stop` を実行する。
func Stop(projectName string) error {
dockerComposeStopCommand := exec.Command("docker", "compose", "-p", projectName, "stop")
return dockerComposeStopCommand.Start()
}

// `docker compose -p ${projectName} down` を実行する。
func Down(projectName string) error {
dockerComposeDownCommand := exec.Command("docker", "compose", "-p", projectName, "down")
Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,29 @@ func main() {
return nil
},
},
{
Name: "stop",
Usage: "Stop devcontainers.",
UsageText: "devcontainer.vim stop WORKSPACE_FOLDER",
HideHelp: true,
SkipFlagParsing: true,
Action: func(cCtx *cli.Context) error {
// devcontainer でコンテナを立てる

// 必要なファイルのダウンロード
devcontainerPath, err := tools.InstallStopTools(binDir)
if err != nil {
panic(err)
}

// devcontainer を用いたコンテナ終了
devcontainer.Stop(cCtx.Args().Slice(), devcontainerPath, configDirForDevcontainer)

fmt.Printf("Stop containers\n")

return nil
},
},
{
Name: "down",
Usage: "Stop and remove devcontainers.",
Expand Down
6 changes: 6 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func InstallTemplatesTools(installDir string) (string, error) {
return devcontainerPath, err
}

// Stop サブコマンド用のツールインストール
func InstallStopTools(installDir string) (string, error) {
devcontainerPath, err := DEVCONTAINER.Install(installDir, false)
return devcontainerPath, err
}

// Down サブコマンド用のツールインストール
func InstallDownTools(installDir string) (string, error) {
devcontainerPath, err := DEVCONTAINER.Install(installDir, false)
Expand Down

0 comments on commit 57a9ea9

Please sign in to comment.