Skip to content

Commit

Permalink
docker-compose.yaml を使っているプロジェクトで、 down できないものがある問題を修正。
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoto2000 committed Jun 21, 2024
1 parent 4423036 commit 6ce54df
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LD_FLAGS := "-s -w -X main.version=${VERSION}"
DEST := ./build

WATCH_SRC := ./main.go \
./devcontainer/DevcontainerJson.go \
./devcontainer/devcontainer.go \
./devcontainer/readConfigurationResult.go \
./devcontainer/upCommandResult.go \
Expand Down
21 changes: 21 additions & 0 deletions devcontainer/DevcontainerJson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package devcontainer

import (
"encoding/json"
)

// devcontainer.json のスキーマ(の一部)
type DevcontainerJSON struct {
DockerComposeFile []string `json:"dockerComposeFile"`
}

func UnmarshalDevcontainerJSON(data []byte) (DevcontainerJSON, error) {
var result DevcontainerJSON

err := json.Unmarshal(data, &result)
if err != nil {
return result, err
}

return result, nil
}
69 changes: 66 additions & 3 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,30 @@ func Stop(args []string, devcontainerPath string, configDirForDevcontainer strin
panic(err)
}

// プロジェクト名を使って docker compose down を実行
// プロジェクト名を使って docker compose stop を実行
fmt.Printf("Run `docker compose -p %s stop`(Async)\n", projectName)

// docker-compose.yaml の格納ディレクトリを探す
dockerComposeFileDir, err := findDockerComposeFileDir()
if err != nil {
panic(err)
}

// カレントディレクトリを記録して dockerComposeFileDir へ移動
currentDir, err := os.Getwd()
if err != nil {
panic(err)
}
os.Chdir(dockerComposeFileDir)

err = dockercompose.Stop(projectName)
if err != nil {
panic(err)
}

// 元のカレントディレクトリへ戻る
os.Chdir(currentDir)

} else {
// ワークスペースに対応するコンテナを探して ID を取得する
containerID, err := docker.GetContainerIDFromWorkspaceFolder(workspaceFolder)
Expand Down Expand Up @@ -228,13 +246,29 @@ func Down(args []string, devcontainerPath string, configDirForDevcontainer strin
panic(err)
}

// docker-compose.yaml の格納ディレクトリを探す
dockerComposeFileDir, err := findDockerComposeFileDir()
if err != nil {
panic(err)
}

// カレントディレクトリを記録して dockerComposeFileDir へ移動
currentDir, err := os.Getwd()
if err != nil {
panic(err)
}
os.Chdir(dockerComposeFileDir)

// プロジェクト名を使って docker compose down を実行
fmt.Printf("Run `docker compose -p %s down`(Async)\n", projectName)
err = dockercompose.Down(projectName)
if err != nil {
panic(err)
}

// 元のカレントディレクトリへ戻る
os.Chdir(currentDir)

// pid ファイル参照のために、
// コンテナ別の設定ファイル格納ディレクトリの名前(コンテナIDを記録)を記録
configDir = util.GetConfigDir(configDirForDevcontainer, workspaceFolder)
Expand Down Expand Up @@ -277,6 +311,35 @@ func Down(args []string, devcontainerPath string, configDirForDevcontainer strin
}
}

// docker-compose.yaml の格納ディレクトリを返却する
func findDockerComposeFileDir() (string, error) {
// devcontainer.json を取得
var devcontainerJSONPath, devcontainerJSONDir string
if util.IsExists(".devcontainer/devcontainer.json") {
devcontainerJSONPath = ".devcontainer/devcontainer.json"
devcontainerJSONDir = filepath.Dir(devcontainerJSONPath)
} else if util.IsExists(".devcontainer.json") {
devcontainerJSONPath = ".devcontainer.json"
devcontainerJSONDir = filepath.Dir(devcontainerJSONPath)
}

// devcontainer.json 読み込み
devcontainerJSONString, err := os.ReadFile(devcontainerJSONPath)
if err != nil {
panic(err)
}

// docker-compose.yaml の格納ディレクトリを組み立て
devcontainerJSON, err := UnmarshalDevcontainerJSON(devcontainerJSONString)
if err != nil {
return "", err
}
dockerComposeFilePath := filepath.Join(devcontainerJSONDir, devcontainerJSON.DockerComposeFile[0])
dockerComposeFileDir := filepath.Dir(dockerComposeFilePath)

return dockerComposeFileDir, nil
}

func GetConfigurationFilePath(devcontainerFilePath string, workspaceFolder string) (string, error) {
stdout, _ := ReadConfiguration(devcontainerFilePath, "--workspace-folder", workspaceFolder)
return GetConfigFilePath(stdout)
Expand All @@ -294,10 +357,10 @@ func ReadConfiguration(devcontainerFilePath string, readConfiguration ...string)
func Templates(
devcontainerFilePath string,
workspaceFolder string,
templateId string) (string, error) {
templateID string) (string, error) {
// コマンドライン引数の末尾は `--workspace-folder` の値として使う

args := []string{"templates", "apply", "--template-id", templateId, "--workspace-folder", workspaceFolder}
args := []string{"templates", "apply", "--template-id", templateID, "--workspace-folder", workspaceFolder}
return ExecuteCombineOutput(devcontainerFilePath, args...)
}

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ func main() {
args := cCtx.Args().Slice()
workspaceFolder := args[len(args)-1]

templateId := selectedItem.ID + ":" + selectedItem.Version
templateID := selectedItem.ID + ":" + selectedItem.Version

// devcontainer を用いたコンテナ立ち上げ
output, _ := devcontainer.Templates(
devcontainerFilePath,
workspaceFolder,
templateId)
templateID)

fmt.Println(output)

Expand Down

0 comments on commit 6ce54df

Please sign in to comment.