diff --git a/Makefile b/Makefile index b2cc999..96eec49 100644 --- a/Makefile +++ b/Makefile @@ -16,12 +16,8 @@ WATCH_SRC := ./main.go \ ./devcontainer/devcontainer.go \ ./devcontainer/run.go \ ./devcontainer/start.go \ - ./devcontainer/dockerRunVimArgs_darwin_arm64.go \ - ./devcontainer/dockerRunVimArgs_linux_amd64.go \ - ./devcontainer/dockerRunVimArgs_windows_amd64.go \ - ./devcontainer/devcontainerStartVimArgs_darwin_arm64.go \ - ./devcontainer/devcontainerStartVimArgs_linux_amd64.go \ - ./devcontainer/devcontainerStartVimArgs_windows_amd64.go \ + ./devcontainer/dockerRunVimArgs.go \ + ./devcontainer/devcontainerStartVimArgs.go \ ./devcontainer/readConfigurationResult.go \ ./devcontainer/upCommandResult.go \ ./docker/docker.go \ @@ -29,12 +25,8 @@ WATCH_SRC := ./main.go \ ./dockercompose/dockerCompose.go \ ./dockercompose/dockerComposePsResult.go \ ./tools/tools.go \ - ./tools/vim_linux_amd64.go \ - ./tools/vim_darwin_arm64.go \ - ./tools/vim_windows_amd64.go \ - ./tools/nvim_linux_amd64.go \ - ./tools/nvim_darwin_arm64.go \ - ./tools/nvim_windows_amd64.go \ + ./tools/vim.go \ + ./tools/nvim.go \ ./tools/devcontainer.go \ ./tools/devcontainer_darwin.go \ ./tools/devcontainer_linux.go \ diff --git a/README.md b/README.md index 5ca299f..e1b0efa 100644 --- a/README.md +++ b/README.md @@ -357,9 +357,10 @@ endif ## Limitation: -- Windows, Linux では、amd64 のコンテナしか使用できません -- amd64 では、 alpine 系のコンテナでは使用できません -- macOS では、arm64 のコンテナしか使用できません +- alpine 系のコンテナでは amd64 アーキテクチャは使用できません +- macOS で NeoVim を利用する場合はシステムインストールされたものを利用する必要があります +- aarch64 のコンテナで NeoVim を利用する場合はシステムインストールされたものを利用する必要があります +- NeoVim AppImage が使えず、システムインストールの NeoVim も存在しない場合、 NeoVim の代わりに Vim が起動します - macOS で NeoVim を利用する場合は、システムインストールの NeoVim しか利用できません システムインストールの NeoVim が検出できなかった場合、代わりに Vim が起動します diff --git a/README_en.md b/README_en.md index bd59882..59af4fb 100644 --- a/README_en.md +++ b/README_en.md @@ -353,11 +353,11 @@ endif ## Limitation: -- On Windows and Linux, only amd64 containers can be used. -- On amd64, alpine-based containers cannot be used. -- On macOS, only arm64 containers can be used. -- If you use NeoVim on macOS, you can only use the system-installed NeoVim. - If a system installed NeoVim cannot be detected, Vim will be launched instead. +- amd64 architecture cannot be used in alpine-based containers +- When using NeoVim in an aarch64 container, you must use the system-installed version +- If the NeoVim AppImage is not available and there is no system-installed NeoVim, Vim will start instead of NeoVim +- When using NeoVim on macOS, only the system-installed NeoVim can be used + If the system-installed NeoVim cannot be detected, Vim will start instead ## Install: diff --git a/devcontainer/devcontainerStartVimArgs.go b/devcontainer/devcontainerStartVimArgs.go new file mode 100644 index 0000000..4706870 --- /dev/null +++ b/devcontainer/devcontainerStartVimArgs.go @@ -0,0 +1,62 @@ +package devcontainer + +import "runtime" + +// `devcontainer.vim start` 時の `devcontainer exec` の引数を組み立てる +// +// Args: +// - containerID: コンテナ ID +// - workspaceFolder: ワークスペースフォルダパス +// - vimFileName: コンテナ上に転送した vim/nvim のファイル名 +// - useSystemVim: true の場合、システムにインストールした vim/nvim を利用する +// +// Return: +// +// `devcontainer exec` に使うコマンドライン引数の配列 +func devcontainerStartVimArgs(containerID string, workspaceFolder string, vimFileName string, sendToTCP string, containerArch string, useSystemVim bool) []string { + if useSystemVim { + return []string{ + "exec", + "--container-id", + containerID, + "--workspace-folder", + workspaceFolder, + "sh", + "-c", + vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } else { + if containerArch == "amd64" { + if runtime.GOOS != "darwin" { + return []string{ + "exec", + "--container-id", + containerID, + "--workspace-folder", + workspaceFolder, + "sh", + "-c", + "cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } else { + return []string{ + "exec", + "--container-id", + containerID, + "--workspace-folder", + workspaceFolder, + "sh", + "-c", + "cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-x86_64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } + } else { + return []string{ + "exec", + "--container-id", + containerID, + "--workspace-folder", + workspaceFolder, + "sh", + "-c", + "cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-aarch64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } + } +} diff --git a/devcontainer/devcontainerStartVimArgs_darwin_arm64.go b/devcontainer/devcontainerStartVimArgs_darwin_arm64.go deleted file mode 100644 index d9d06c8..0000000 --- a/devcontainer/devcontainerStartVimArgs_darwin_arm64.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build darwin && arm64 - -package devcontainer - -// `devcontainer.vim start` 時の `devcontainer exec` の引数を組み立てる -// -// Args: -// - containerID: コンテナ ID -// - workspaceFolder: ワークスペースフォルダパス -// - vimFileName: コンテナ上に転送した vim/nvim のファイル名 -// - useSystemVim: true の場合、システムにインストールした vim/nvim を利用する -// -// Return: -// -// `devcontainer exec` に使うコマンドライン引数の配列 -func devcontainerStartVimArgs(containerID string, workspaceFolder string, vimFileName string, sendToTCP string, useSystemVim bool) []string { - if useSystemVim { - return []string{ - "exec", - "--container-id", - containerID, - "--workspace-folder", - workspaceFolder, - "sh", - "-c", - vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } else { - return []string{ - "exec", - "--container-id", - containerID, - "--workspace-folder", - workspaceFolder, - "sh", - "-c", - "cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-aarch64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } -} diff --git a/devcontainer/devcontainerStartVimArgs_linux_amd64.go b/devcontainer/devcontainerStartVimArgs_linux_amd64.go deleted file mode 100644 index c65d547..0000000 --- a/devcontainer/devcontainerStartVimArgs_linux_amd64.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build linux && amd64 - -package devcontainer - -// `devcontainer.vim start` 時の `devcontainer exec` の引数を組み立てる -// -// Args: -// - containerID: コンテナ ID -// - workspaceFolder: ワークスペースフォルダパス -// - vimFileName: コンテナ上に転送した vim/nvim のファイル名 -// - useSystemVim: true の場合、システムにインストールした vim/nvim を利用する -// -// Return: -// -// `devcontainer exec` に使うコマンドライン引数の配列 -func devcontainerStartVimArgs(containerID string, workspaceFolder string, vimFileName string, sendToTCP string, useSystemVim bool) []string { - if useSystemVim { - return []string{ - "exec", - "--container-id", - containerID, - "--workspace-folder", - workspaceFolder, - "sh", - "-c", - vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } else { - return []string{ - "exec", - "--container-id", - containerID, - "--workspace-folder", - workspaceFolder, - "sh", - "-c", - "cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } -} diff --git a/devcontainer/devcontainerStartVimArgs_windows_amd64.go b/devcontainer/devcontainerStartVimArgs_windows_amd64.go deleted file mode 100644 index ba008fb..0000000 --- a/devcontainer/devcontainerStartVimArgs_windows_amd64.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build windows && amd64 - -package devcontainer - -// `devcontainer.vim start` 時の `devcontainer exec` の引数を組み立てる -// -// Args: -// - containerID: コンテナ ID -// - workspaceFolder: ワークスペースフォルダパス -// - vimFileName: コンテナ上に転送した vim/nvim のファイル名 -// - useSystemVim: true の場合、システムにインストールした vim/nvim を利用する -// -// Return: -// -// `devcontainer exec` に使うコマンドライン引数の配列 -func devcontainerStartVimArgs(containerID string, workspaceFolder string, vimFileName string, sendToTCP string, useSystemVim bool) []string { - if useSystemVim { - return []string{ - "exec", - "--container-id", - containerID, - "--workspace-folder", - workspaceFolder, - "sh", - "-c", - vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } else { - return []string{ - "exec", - "--container-id", - containerID, - "--workspace-folder", - workspaceFolder, - "sh", - "-c", - "cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } -} diff --git a/devcontainer/dockerRunVimArgs.go b/devcontainer/dockerRunVimArgs.go new file mode 100644 index 0000000..ed06068 --- /dev/null +++ b/devcontainer/dockerRunVimArgs.go @@ -0,0 +1,53 @@ +package devcontainer + +import "runtime" + +// `devcontainer.vim run` 時の `docker exec` の引数を組み立てる +// +// Args: +// - containerID: コンテナ ID +// - vimFileName: コンテナ上に転送した vim のファイル名 +// - useSystemVim: true の場合、システムにインストールされた vim/nvim を使用する +// +// Return: +// +// `docker exec` に使うコマンドライン引数の配列 +func dockerRunVimArgs(containerID string, vimFileName string, sendToTCP string, containerArch string, useSystemVim bool) []string { + if useSystemVim { + return []string{ + "exec", + "-it", + containerID, + "sh", + "-c", + vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } else { + if containerArch == "amd64" { + if runtime.GOOS != "darwin" { + return []string{ + "exec", + "-it", + containerID, + "sh", + "-c", + "cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } else { + return []string{ + "exec", + "-it", + containerID, + "sh", + "-c", + "cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-x86_64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } + } else { + return []string{ + "exec", + "-it", + containerID, + "sh", + "-c", + "cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-aarch64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} + } + } +} diff --git a/devcontainer/dockerRunVimArgs_darwin_arm64.go b/devcontainer/dockerRunVimArgs_darwin_arm64.go deleted file mode 100644 index 8a159a0..0000000 --- a/devcontainer/dockerRunVimArgs_darwin_arm64.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build darwin && arm64 - -package devcontainer - -// `devcontainer.vim run` 時の `docker exec` の引数を組み立てる -// -// Args: -// - containerID: コンテナ ID -// - vimFileName: コンテナ上に転送した vim/nvim のファイル名 -// - useSystemVim: true の場合、システムにインストールされた vim/nvim を使用する -// -// Return: -// -// `docker exec` に使うコマンドライン引数の配列 -func dockerRunVimArgs(containerID string, vimFileName string, sendToTCP string, useSystemVim bool) []string { - if useSystemVim { - return []string{ - "exec", - "-it", - containerID, - "sh", - "-c", - vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } else { - return []string{ - "exec", - "-it", - containerID, - "sh", - "-c", - "cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-aarch64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } -} diff --git a/devcontainer/dockerRunVimArgs_linux_amd64.go b/devcontainer/dockerRunVimArgs_linux_amd64.go deleted file mode 100644 index 7ac8b1a..0000000 --- a/devcontainer/dockerRunVimArgs_linux_amd64.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build linux && amd64 - -package devcontainer - -// `devcontainer.vim run` 時の `docker exec` の引数を組み立てる -// -// Args: -// - containerID: コンテナ ID -// - vimFileName: コンテナ上に転送した vim のファイル名 -// - useSystemVim: true の場合、システムにインストールされた vim/nvim を使用する -// -// Return: -// -// `docker exec` に使うコマンドライン引数の配列 -func dockerRunVimArgs(containerID string, vimFileName string, sendToTCP string, useSystemVim bool) []string { - if useSystemVim { - return []string{ - "exec", - "-it", - containerID, - "sh", - "-c", - vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } else { - return []string{ - "exec", - "-it", - containerID, - "sh", - "-c", - "cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } -} diff --git a/devcontainer/dockerRunVimArgs_windows_amd64.go b/devcontainer/dockerRunVimArgs_windows_amd64.go deleted file mode 100644 index 567098f..0000000 --- a/devcontainer/dockerRunVimArgs_windows_amd64.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build windows && amd64 - -package devcontainer - -// `devcontainer.vim run` 時の `docker exec` の引数を組み立てる -// -// Args: -// - containerID: コンテナ ID -// - vimFileName: コンテナ上に転送した vim のファイル名 -// - useSystemVim: true の場合、システムにインストールされた vim/nvim を使用する -// -// Return: -// -// `docker exec` に使うコマンドライン引数の配列 -func dockerRunVimArgs(containerID string, vimFileName string, sendToTCP string, useSystemVim bool) []string { - if useSystemVim { - return []string{ - "exec", - "-it", - containerID, - "sh", - "-c", - vimFileName + " --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } else { - return []string{ - "exec", - "-it", - containerID, - "sh", - "-c", - "cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /" + sendToTCP + " -S /vimrc"} - } -} diff --git a/devcontainer/run.go b/devcontainer/run.go index 53860ec..4a090cd 100644 --- a/devcontainer/run.go +++ b/devcontainer/run.go @@ -12,6 +12,7 @@ import ( "github.com/mikoto2000/devcontainer.vim/v3/docker" "github.com/mikoto2000/devcontainer.vim/v3/tools" + "github.com/mikoto2000/devcontainer.vim/v3/util" ) var devcontainerRunArgsPrefix = []string{"run", "-d", "--rm"} @@ -34,8 +35,7 @@ func (e *ChmodError) Error() string { } // docker run で、ワンショットでコンテナを立ち上げる -func Run(args []string, vimFilePath string, cdrPath string, configDirForDocker string, vimrc string, defaultRunargs []string) error { - vimFileName := filepath.Base(vimFilePath) +func Run(args []string, cdrPath string, vimInstallDir string, nvim bool, configDirForDocker string, vimrc string, defaultRunargs []string) error { // バックグラウンドでコンテナを起動 // `docker run -d --rm os.Args[1:] sh -c "sleep infinity"` @@ -59,6 +59,25 @@ func Run(args []string, vimFilePath string, cdrPath string, configDirForDocker s containerID = strings.ReplaceAll(containerID, "\r", "") fmt.Printf("Container started. id: %s\n", containerID) + // コンテナ内に入り、コンテナの Arch を確認 + containerArch, err := docker.Exec(containerID, "uname", "-m") + if err != nil { + return err + } + containerArch = strings.TrimSpace(containerArch) + containerArch, err = util.NormalizeContainerArch(containerArch) + if err != nil { + return err + } + fmt.Printf("Container Arch: '%s'.\n", containerArch) + + vimFilePath, err := tools.InstallVim(vimInstallDir, nvim, containerArch) + if err != nil { + return err + } + + vimFileName := filepath.Base(vimFilePath) + // clipboard-data-receiver を起動 configDirForCdr := filepath.Join(configDirForDocker, containerID) err = os.MkdirAll(configDirForCdr, 0744) @@ -71,9 +90,6 @@ func Run(args []string, vimFilePath string, cdrPath string, configDirForDocker s } fmt.Printf("Started clipboard-data-receiver with pid: %d, port: %d\n", pid, port) - // コンテナへ appimage を転送して実行権限を追加 - // `docker cp :/` - useSystemVim := false fmt.Printf("Check system installed %s ... ", vimFileName) out, _ := docker.Exec(containerID, "which", vimFileName) @@ -83,15 +99,23 @@ func Run(args []string, vimFilePath string, cdrPath string, configDirForDocker s } else { fmt.Printf("not found.\n") - // arm の場合スタティックリンクの nvim を作れないため、 vim にフォールバック if runtime.GOARCH == "arm64" { + // arm の場合スタティックリンクの nvim を作れないため、 vim にフォールバック vimFileName = "vim" + nvim = false + } else if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" && nvim { + // M1 Mac で amd64 のコンテナを動かすと、なぜか AppImage が動かないので vim にフォールバック + vimFileName = "vim" + nvim = false } } fmt.Printf("docker exec output: \"%s\".\n", strings.TrimSpace(out)) if !useSystemVim { - err = docker.Cp("vim", vimFilePath, containerID, "/" + vimFileName) + // コンテナへ appimage を転送して実行権限を追加 + // `docker cp :/` + + err = docker.Cp("vim", vimFilePath, containerID, "/"+vimFileName) if err != nil { return err } @@ -133,7 +157,7 @@ func Run(args []string, vimFilePath string, cdrPath string, configDirForDocker s defer cancel() sendToTCPName := filepath.Base(sendToTCP) - dockerRunVimArgs := dockerRunVimArgs(containerID, vimFileName, sendToTCPName, useSystemVim) + dockerRunVimArgs := dockerRunVimArgs(containerID, vimFileName, sendToTCPName, containerArch, useSystemVim) fmt.Printf("Start vim: `%s \"%s\"`\n", containerCommand, strings.Join(dockerRunVimArgs, "\" \"")) dockerExec := exec.CommandContext(ctx, containerCommand, dockerRunVimArgs...) dockerExec.Stdin = os.Stdin diff --git a/devcontainer/start.go b/devcontainer/start.go index 46e6275..c8775fd 100644 --- a/devcontainer/start.go +++ b/devcontainer/start.go @@ -12,15 +12,14 @@ import ( "github.com/mikoto2000/devcontainer.vim/v3/docker" "github.com/mikoto2000/devcontainer.vim/v3/tools" + "github.com/mikoto2000/devcontainer.vim/v3/util" ) var devcontainreArgsPrefix = []string{"up"} // devcontainer でコンテナを立ち上げ、 Vim を転送し、実行する。 // 既存実装の都合上、configFilePath から configDirForDevcontainer を抽出している -func Start(args []string, devcontainerPath string, vimFilePath string, cdrPath, configFilePath string, vimrc string) error { - - vimFileName := filepath.Base(vimFilePath) +func Start(args []string, devcontainerPath string, cdrPath, vimInstallDir string, nvim bool, configFilePath string, vimrc string) error { // コマンドライン引数の末尾は `--workspace-folder` の値として使う workspaceFolder := args[len(args)-1] @@ -55,10 +54,29 @@ func Start(args []string, devcontainerPath string, vimFilePath string, cdrPath, } fmt.Printf("Started clipboard-data-receiver with pid: %d, port: %d\n", pid, port) - // コンテナへ appimage を転送して実行権限を追加 - // `docker cp :/` containerID := upCommandResult.ContainerID + // コンテナ内に入り、コンテナの Arch を確認 + containerArch, err := docker.Exec(containerID, "uname", "-m") + if err != nil { + return err + } + containerArch = strings.TrimSpace(containerArch) + containerArch, err = util.NormalizeContainerArch(containerArch) + if err != nil { + return err + } + fmt.Printf("Container Arch: '%s'.\n", containerArch) + + vimFilePath, err := tools.InstallVim(vimInstallDir, nvim, containerArch) + if err != nil { + return err + } + + // vim_, nvim_ の形式でパスがわたってくるので、 + // vim/nvim の部分を抽出する。 + vimFileName := strings.Split(filepath.Base(vimFilePath), "_")[0] + useSystemVim := false fmt.Printf("Check system installed %s ... ", vimFileName) out, _ := docker.Exec(containerID, "which", vimFileName) @@ -68,15 +86,22 @@ func Start(args []string, devcontainerPath string, vimFilePath string, cdrPath, } else { fmt.Printf("not found.\n") - // arm の場合スタティックリンクの nvim を作れないため、 vim にフォールバック if runtime.GOARCH == "arm64" { + // arm の場合スタティックリンクの nvim を作れないため、 vim にフォールバック + vimFileName = "vim" + nvim = false + } else if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" && nvim { + // M1 Mac で amd64 のコンテナを動かすと、なぜか AppImage が動かないので vim にフォールバック vimFileName = "vim" + nvim = false } } fmt.Printf("docker exec output: \"%s\".\n", strings.TrimSpace(out)) if !useSystemVim { - err = docker.Cp("vim", vimFilePath, containerID, "/" + vimFileName) + // コンテナへ appimage を転送して実行権限を追加 + // `docker cp :/` + err = docker.Cp("vim", vimFilePath, containerID, "/"+vimFileName) if err != nil { return err } @@ -118,7 +143,7 @@ func Start(args []string, devcontainerPath string, vimFilePath string, cdrPath, defer cancel() sendToTCPName := filepath.Base(sendToTCP) - devcontainerStartVimArgs := devcontainerStartVimArgs(containerID, workspaceFolder, vimFileName, sendToTCPName, useSystemVim) + devcontainerStartVimArgs := devcontainerStartVimArgs(containerID, workspaceFolder, vimFileName, sendToTCPName, containerArch, useSystemVim) fmt.Printf("Start vim: `%s \"%s\"`\n", devcontainerPath, strings.Join(devcontainerStartVimArgs, "\" \"")) dockerExec := exec.CommandContext(ctx, devcontainerPath, devcontainerStartVimArgs...) dockerExec.Stdin = os.Stdin diff --git a/main.go b/main.go index 92d1058..6378dd6 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ const envDevcontainerVimType = "DEVCONTAINER_VIM_TYPE" const flagNameLicense = "license" const flagNameNeoVim = "nvim" +const flagNameArch = "arch" const flagNameGenerate = "generate" const flagNameHome = "home" @@ -162,7 +163,7 @@ func main() { if cCtx.Bool(flagNameNeoVim) || os.Getenv(envDevcontainerVimType) == "nvim" { nvim = true } - vimPath, cdrPath, err := tools.InstallRunTools(binDir, nvim) + cdrPath, err := tools.InstallRunTools(binDir, nvim) if err != nil { fmt.Fprintf(os.Stderr, "Error installing run tools: %v\n", err) os.Exit(1) @@ -179,7 +180,7 @@ func main() { if runtime.GOOS == "windows" { // コンテナ起動 // windows はシェル変数展開が上手くいかないので runargs を使用しない - err = devcontainer.Run(cCtx.Args().Slice(), vimPath, cdrPath, configDirForDocker, vimrc, []string{}) + err = devcontainer.Run(cCtx.Args().Slice(), cdrPath, binDir, nvim, configDirForDocker, vimrc, []string{}) if err != nil { fmt.Fprintf(os.Stderr, "Error running docker: %v\n", err) os.Exit(1) @@ -200,7 +201,7 @@ func main() { } // コンテナ起動 - err = devcontainer.Run(cCtx.Args().Slice(), vimPath, cdrPath, configDirForDocker, vimrc, defaultRunargs) + err = devcontainer.Run(cCtx.Args().Slice(), cdrPath, binDir, nvim, configDirForDocker, vimrc, defaultRunargs) if err != nil { fmt.Fprintf(os.Stderr, "Error running docker: %v\n", err) os.Exit(1) @@ -324,7 +325,7 @@ func main() { if cCtx.Bool(flagNameNeoVim) || os.Getenv(envDevcontainerVimType) == "nvim" { nvim = true } - vimPath, devcontainerPath, cdrPath, err := tools.InstallStartTools(binDir, nvim) + devcontainerPath, cdrPath, err := tools.InstallStartTools(binDir) if err != nil { fmt.Fprintf(os.Stderr, "Error installing start tools: %v\n", err) os.Exit(1) @@ -344,7 +345,7 @@ func main() { } // devcontainer を用いたコンテナ立ち上げ - err = devcontainer.Start(args, devcontainerPath, vimPath, cdrPath, configFilePath, vimrc) + err = devcontainer.Start(args, devcontainerPath, cdrPath, binDir, nvim, configFilePath, vimrc) if err != nil { if errors.Is(err, os.ErrPermission) { fmt.Fprintf(os.Stderr, "Permission error: %v\n", err) @@ -624,10 +625,17 @@ func main() { UsageText: "devcontainer.vim tool vim download", HideHelp: false, SkipFlagParsing: false, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: flagNameArch, + Value: runtime.GOARCH, + Usage: "regenerate runargs file.", + }, + }, Action: func(cCtx *cli.Context) error { // Vim のダウンロード - _, err := tools.VIM.Install(binDir, true) + _, err := tools.VIM.Install(binDir, cCtx.String(flagNameArch), true) if err != nil { fmt.Fprintf(os.Stderr, "Error installing vim: %v\n", err) os.Exit(1) @@ -651,10 +659,17 @@ func main() { UsageText: "devcontainer.vim tool nvim download", HideHelp: false, SkipFlagParsing: false, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: flagNameArch, + Value: runtime.GOARCH, + Usage: "regenerate runargs file.", + }, + }, Action: func(cCtx *cli.Context) error { // NeoVim のダウンロード - _, err := tools.NVIM.Install(binDir, true) + _, err := tools.NVIM.Install(binDir, cCtx.String(flagNameArch), true) if err != nil { fmt.Fprintf(os.Stderr, "Error installing nvim: %v\n", err) os.Exit(1) @@ -681,7 +696,7 @@ func main() { Action: func(cCtx *cli.Context) error { // devcontainer のダウンロード - _, err := tools.DEVCONTAINER.Install(binDir, true) + _, err := tools.DEVCONTAINER.Install(binDir, "", true) if err != nil { fmt.Fprintf(os.Stderr, "Error installing devcontainer: %v\n", err) os.Exit(1) @@ -708,7 +723,7 @@ func main() { Action: func(cCtx *cli.Context) error { // clipboard-data-receiver のダウンロード - _, err := tools.CDR.Install(binDir, true) + _, err := tools.CDR.Install(binDir, "", true) if err != nil { fmt.Fprintf(os.Stderr, "Error installing clipboard-data-receiver: %v\n", err) os.Exit(1) diff --git a/tools/clipboard-data-receiver.go b/tools/clipboard-data-receiver.go index 84ce08e..6bb2ef9 100644 --- a/tools/clipboard-data-receiver.go +++ b/tools/clipboard-data-receiver.go @@ -99,21 +99,21 @@ var CDR Tool = func() Tool { // 実際に使用する cdr の構造体を返却 return Tool{ FileName: cdrFileName, - CalculateDownloadURL: func() string { + CalculateDownloadURL: func(_ string) (string, error) { latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "clipboard-data-receiver") if err != nil { - panic(err) + return "", err } tmplParams := map[string]string{"TagName": latestTagName} var downloadURL strings.Builder err = tmpl.Execute(&downloadURL, tmplParams) if err != nil { - panic(err) + return "", err } - return downloadURL.String() + return downloadURL.String(), nil }, - installFunc: func(downloadURL string, filePath string) (string, error) { + installFunc: func(downloadURL string, filePath string, _ string) (string, error) { return simpleInstall(downloadURL, filePath) }, } diff --git a/tools/devcontainer.go b/tools/devcontainer.go index d0f4a23..323bb72 100644 --- a/tools/devcontainer.go +++ b/tools/devcontainer.go @@ -10,27 +10,27 @@ import ( // devcontainer/cli のツール情報 var DEVCONTAINER Tool = Tool{ FileName: devcontainerFileName, - CalculateDownloadURL: func() string { + CalculateDownloadURL: func(_ string) (string, error) { latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "devcontainers-cli") if err != nil { - panic(err) + return "", err } pattern := "pattern" tmpl, err := template.New(pattern).Parse(downloadURLDevcontainersCliPattern) if err != nil { - panic(err) + return "", err } tmplParams := map[string]string{"TagName": latestTagName} var downloadURL strings.Builder err = tmpl.Execute(&downloadURL, tmplParams) if err != nil { - panic(err) + return "", err } - return downloadURL.String() + return downloadURL.String(), nil }, - installFunc: func(downloadURL string, filePath string) (string, error) { + installFunc: func(downloadURL string, filePath string, _ string) (string, error) { return simpleInstall(downloadURL, filePath) }, } diff --git a/tools/nvim.go b/tools/nvim.go new file mode 100644 index 0000000..616e349 --- /dev/null +++ b/tools/nvim.go @@ -0,0 +1,64 @@ +package tools + +import ( + "errors" + "strings" + "text/template" + + "github.com/mikoto2000/devcontainer.vim/v3/util" +) + +// NeoVim のダウンロード URL +const nvimAppImageDownloadURLPattern = "https://github.com/neovim/neovim/releases/download/{{ .TagName }}/nvim.appimage" +const nvimArmStaticDownloadURLPattern = "https://github.com/mikoto2000/vim-static/releases/download/{{ .TagName }}/vim-{{ .TagName }}-aarch64.tar.gz" + +// NeoVim のツール情報 +var NVIM Tool = Tool{ + FileName: "nvim", + CalculateDownloadURL: func(containerArch string) (string, error) { + if containerArch == "amd64" || containerArch == "x86_64" { + latestTagName, err := util.GetLatestReleaseFromGitHub("neovim", "neovim") + if err != nil { + return "", err + } + + pattern := "pattern" + tmpl, err := template.New(pattern).Parse(nvimAppImageDownloadURLPattern) + if err != nil { + return "", err + } + + tmplParams := map[string]string{"TagName": latestTagName} + var downloadURL strings.Builder + err = tmpl.Execute(&downloadURL, tmplParams) + if err != nil { + return "", err + } + return downloadURL.String(), nil + } else if containerArch == "arm64" || containerArch == "aarch64" { + latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "vim-static") + if err != nil { + return "", err + } + + pattern := "pattern" + tmpl, err := template.New(pattern).Parse(nvimArmStaticDownloadURLPattern) + if err != nil { + return "", err + } + + tmplParams := map[string]string{"TagName": latestTagName} + var downloadURL strings.Builder + err = tmpl.Execute(&downloadURL, tmplParams) + if err != nil { + return "", err + } + return downloadURL.String(), nil + } else { + return "", errors.New("Unknown Architecture") + } + }, + installFunc: func(downloadURL string, filePath string, containerArch string) (string, error) { + return simpleInstall(downloadURL, filePath) + }, +} diff --git a/tools/nvim_darwin_arm64.go b/tools/nvim_darwin_arm64.go deleted file mode 100644 index 2696941..0000000 --- a/tools/nvim_darwin_arm64.go +++ /dev/null @@ -1,60 +0,0 @@ -//go:build darwin - -// ARM 版スタティックリンク NeoVim が存在しないため、 -// Vim のスタティックリンクバイナリで我慢してもらう。 - -package tools - -import ( - "strings" - "text/template" - - "github.com/mikoto2000/devcontainer.vim/v3/util" -) - -// Vim のダウンロード URL -// システムインストールの NeoVim が見つからなかった場合に Vim を起動するため、 -// バイナリダウンロートが必要ない。 -// TODO: 余計なダウンロードなので、発生しないようにしたい -const nvimDownloadURLPattern = "https://github.com/mikoto2000/vim-static/releases/download/{{ .TagName }}/vim-{{ .TagName }}-aarch64.tar.gz" - -// Vim のツール情報 -var NVIM Tool = Tool{ - FileName: "nvim", - CalculateDownloadURL: func() string { - latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "vim-static") - if err != nil { - panic(err) - } - - pattern := "pattern" - tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern) - if err != nil { - panic(err) - } - - tmplParams := map[string]string{"TagName": latestTagName} - var downloadURL strings.Builder - err = tmpl.Execute(&downloadURL, tmplParams) - if err != nil { - panic(err) - } - return downloadURL.String() - }, - installFunc: func(downloadURL string, filePath string) (string, error) { - _, err := simpleInstall(downloadURL, filePath) - if err != nil { - return "", err - } - - // TODO, tar.gz を展開 - //err = util.ExtractTarGz(filePath + ".tar.gz" ,filePath) - //if err != nil { - // return "", err - //} - return filePath, nil - - }, -} - - diff --git a/tools/nvim_linux_amd64.go b/tools/nvim_linux_amd64.go deleted file mode 100644 index 3725344..0000000 --- a/tools/nvim_linux_amd64.go +++ /dev/null @@ -1,41 +0,0 @@ -//go:build linux && amd64 - -package tools - -import ( - "strings" - "text/template" - - "github.com/mikoto2000/devcontainer.vim/v3/util" -) - -// NeoVim のダウンロード URL -const nvimDownloadURLPattern = "https://github.com/neovim/neovim/releases/download/{{ .TagName }}/nvim.appimage" - -// NeoVim のツール情報 -var NVIM Tool = Tool{ - FileName: "nvim", - CalculateDownloadURL: func() string { - latestTagName, err := util.GetLatestReleaseFromGitHub("neovim", "neovim") - if err != nil { - panic(err) - } - - pattern := "pattern" - tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern) - if err != nil { - panic(err) - } - - tmplParams := map[string]string{"TagName": latestTagName} - var downloadURL strings.Builder - err = tmpl.Execute(&downloadURL, tmplParams) - if err != nil { - panic(err) - } - return downloadURL.String() - }, - installFunc: func(downloadURL string, filePath string) (string, error) { - return simpleInstall(downloadURL, filePath) - }, -} diff --git a/tools/nvim_windows_amd64.go b/tools/nvim_windows_amd64.go deleted file mode 100644 index 3356f31..0000000 --- a/tools/nvim_windows_amd64.go +++ /dev/null @@ -1,41 +0,0 @@ -//go:build windows && amd64 - -package tools - -import ( - "strings" - "text/template" - - "github.com/mikoto2000/devcontainer.vim/v3/util" -) - -// NeoVim のダウンロード URL -const nvimDownloadURLPattern = "https://github.com/neovim/neovim/releases/download/{{ .TagName }}/nvim.appimage" - -// NeoVim のツール情報 -var NVIM Tool = Tool{ - FileName: "nvim", - CalculateDownloadURL: func() string { - latestTagName, err := util.GetLatestReleaseFromGitHub("neovim", "neovim") - if err != nil { - panic(err) - } - - pattern := "pattern" - tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern) - if err != nil { - panic(err) - } - - tmplParams := map[string]string{"TagName": latestTagName} - var downloadURL strings.Builder - err = tmpl.Execute(&downloadURL, tmplParams) - if err != nil { - panic(err) - } - return downloadURL.String() - }, - installFunc: func(downloadURL string, filePath string) (string, error) { - return simpleInstall(downloadURL, filePath) - }, -} diff --git a/tools/tools.go b/tools/tools.go index c98b2a9..bd05c64 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -14,21 +14,37 @@ import ( // ツール情報 type Tool struct { FileName string - CalculateDownloadURL func() string - installFunc func(downloadURL string, filePath string) (string, error) + CalculateDownloadURL func(containerArch string) (string, error) + installFunc func(downloadURL string, filePath string, containerArch string) (string, error) } // ツールのインストールを実行 -func (t Tool) Install(installDir string, override bool) (string, error) { +func (t Tool) Install(installDir string, containerArch string, override bool) (string, error) { + + // tool download から直接呼ばれることもあるのでここでも正規化する + containerArch, err := util.NormalizeContainerArch(containerArch) + if err != nil { + return "", nil + } // ツールの配置先組み立て - filePath := filepath.Join(installDir, t.FileName) + var fileName string + if containerArch != "" { + fileName = t.FileName + "_" + containerArch + } else { + fileName = t.FileName + } + filePath := filepath.Join(installDir, fileName) if util.IsExists(filePath) && !override { fmt.Printf("%s aleady exist, use this.\n", filePath) return filePath, nil } else { - return t.installFunc(t.CalculateDownloadURL(), filePath) + downloadURL, err := t.CalculateDownloadURL(containerArch) + if err != nil { + return "", err + } + return t.installFunc(downloadURL, filePath, containerArch) } } @@ -109,69 +125,67 @@ func download(downloadURL string, destPath string) error { } // run サブコマンド用のツールインストール -func InstallRunTools(installDir string, nvim bool) (string, string, error) { - var vimPath string +func InstallRunTools(installDir string, nvim bool) (string, error) { var err error - if !nvim { - vimPath, err = VIM.Install(installDir, false) - } else { - vimPath, err = NVIM.Install(installDir, false) - } + cdrPath, err := CDR.Install(installDir, "", false) if err != nil { - return vimPath, "", err + return cdrPath, err } - cdrPath, err := CDR.Install(installDir, false) - if err != nil { - return vimPath, cdrPath, err - } - return vimPath, cdrPath, err + return cdrPath, err } -// start サブコマンド用のツールインストール -// 戻り値は、 vimPath, devcontainerPath, cdrPath, error -func InstallStartTools(installDir string, nvim bool) (string, string, string, error) { +func InstallVim(installDir string, nvim bool, containerArch string) (string, error) { var vimPath string var err error if !nvim { - vimPath, err = VIM.Install(installDir, false) + vimPath, err = VIM.Install(installDir, containerArch, false) } else { - vimPath, err = NVIM.Install(installDir, false) - } - if err != nil { - return vimPath, "", "", err - } - devcontainerPath, err := DEVCONTAINER.Install(installDir, false) + if runtime.GOOS == "darwin" && containerArch == "amd64" { + // M1 Mac で amd64 のコンテナを動かすと、なぜか AppImage が動かないので vim にフォールバック + vimPath, err = VIM.Install(installDir, containerArch, false) + } else { + vimPath, err = NVIM.Install(installDir, containerArch, false) + } + } + return vimPath, err +} + +// start サブコマンド用のツールインストール +// 戻り値は、 devcontainerPath, cdrPath, error +func InstallStartTools(installDir string) (string, string, error) { + var err error + devcontainerPath, err := DEVCONTAINER.Install(installDir, "", false) if err != nil { - return vimPath, devcontainerPath, "", err + return devcontainerPath, "", err } - cdrPath, err := CDR.Install(installDir, false) + cdrPath, err := CDR.Install(installDir, "", false) if err != nil { - return vimPath, devcontainerPath, cdrPath, err + return devcontainerPath, cdrPath, err } - return vimPath, devcontainerPath, cdrPath, err + return devcontainerPath, cdrPath, err } // devcontainer サブコマンド用のツールインストール func InstallDevcontainerTools(installDir string) (string, error) { - devcontainerPath, err := DEVCONTAINER.Install(installDir, false) + devcontainerPath, err := DEVCONTAINER.Install(installDir, "", false) return devcontainerPath, err } // Templates サブコマンド用のツールインストール func InstallTemplatesTools(installDir string) (string, error) { - devcontainerPath, err := DEVCONTAINER.Install(installDir, false) + devcontainerPath, err := DEVCONTAINER.Install(installDir, "", false) return devcontainerPath, err } // Stop サブコマンド用のツールインストール func InstallStopTools(installDir string) (string, error) { - devcontainerPath, err := DEVCONTAINER.Install(installDir, false) + devcontainerPath, err := DEVCONTAINER.Install(installDir, "", false) return devcontainerPath, err } // Down サブコマンド用のツールインストール func InstallDownTools(installDir string) (string, error) { - devcontainerPath, err := DEVCONTAINER.Install(installDir, false) + devcontainerPath, err := DEVCONTAINER.Install(installDir, "", false) return devcontainerPath, err } diff --git a/tools/vim.go b/tools/vim.go new file mode 100644 index 0000000..4c41324 --- /dev/null +++ b/tools/vim.go @@ -0,0 +1,87 @@ +package tools + +import ( + "errors" + "runtime" + "strings" + "text/template" + + "github.com/mikoto2000/devcontainer.vim/v3/util" +) + +// Vim のダウンロード URL +const vimAppImageDownloadURLPattern = "https://github.com/vim/vim-appimage/releases/download/{{ .TagName }}/Vim-{{ .TagName }}.glibc2.29-x86_64.AppImage" +const vimX64StaticDownloadURLPattern = "https://github.com/mikoto2000/vim-static/releases/download/{{ .TagName }}/vim-{{ .TagName }}-x86_64.tar.gz" +const vimArmStaticDownloadURLPattern = "https://github.com/mikoto2000/vim-static/releases/download/{{ .TagName }}/vim-{{ .TagName }}-aarch64.tar.gz" + +// Vim のツール情報 +var VIM Tool = Tool{ + FileName: "vim", + CalculateDownloadURL: func(containerArch string) (string, error) { + if containerArch == "amd64" || containerArch == "x86_64" { + if runtime.GOOS != "darwin" { + latestTagName, err := util.GetLatestReleaseFromGitHub("vim", "vim-appimage") + if err != nil { + return "", err + } + + pattern := "pattern" + tmpl, err := template.New(pattern).Parse(vimAppImageDownloadURLPattern) + if err != nil { + return "", err + } + + tmplParams := map[string]string{"TagName": latestTagName} + var downloadURL strings.Builder + err = tmpl.Execute(&downloadURL, tmplParams) + if err != nil { + return "", err + } + return downloadURL.String(), nil + } else { + latestTagName, err := util.GetLatestReleaseFromGitHub("vim", "vim-appimage") + if err != nil { + return "", err + } + + pattern := "pattern" + tmpl, err := template.New(pattern).Parse(vimX64StaticDownloadURLPattern) + if err != nil { + return "", err + } + + tmplParams := map[string]string{"TagName": latestTagName} + var downloadURL strings.Builder + err = tmpl.Execute(&downloadURL, tmplParams) + if err != nil { + return "", err + } + return downloadURL.String(), nil + } + } else if containerArch == "arm64" || containerArch == "aarch64" { + latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "vim-static") + if err != nil { + return "", err + } + + pattern := "pattern" + tmpl, err := template.New(pattern).Parse(vimArmStaticDownloadURLPattern) + if err != nil { + return "", err + } + + tmplParams := map[string]string{"TagName": latestTagName} + var downloadURL strings.Builder + err = tmpl.Execute(&downloadURL, tmplParams) + if err != nil { + return "", err + } + return downloadURL.String(), nil + } else { + return "", errors.New("Unknown Architecture") + } + }, + installFunc: func(downloadURL string, filePath string, containerArch string) (string, error) { + return simpleInstall(downloadURL, filePath) + }, +} diff --git a/tools/vim_darwin_arm64.go b/tools/vim_darwin_arm64.go deleted file mode 100644 index 28074dd..0000000 --- a/tools/vim_darwin_arm64.go +++ /dev/null @@ -1,53 +0,0 @@ -//go:build darwin - -package tools - -import ( - "strings" - "text/template" - - "github.com/mikoto2000/devcontainer.vim/v3/util" -) - -// Vim のダウンロード URL -const vimDownloadURLPattern = "https://github.com/mikoto2000/vim-static/releases/download/{{ .TagName }}/vim-{{ .TagName }}-aarch64.tar.gz" - -// Vim のツール情報 -var VIM Tool = Tool{ - FileName: "vim", - CalculateDownloadURL: func() string { - latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "vim-static") - if err != nil { - panic(err) - } - - pattern := "pattern" - tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern) - if err != nil { - panic(err) - } - - tmplParams := map[string]string{"TagName": latestTagName} - var downloadURL strings.Builder - err = tmpl.Execute(&downloadURL, tmplParams) - if err != nil { - panic(err) - } - return downloadURL.String() - }, - installFunc: func(downloadURL string, filePath string) (string, error) { - _, err := simpleInstall(downloadURL, filePath) - if err != nil { - return "", err - } - - // TODO, tar.gz を展開 - //err = util.ExtractTarGz(filePath + ".tar.gz" ,filePath) - //if err != nil { - // return "", err - //} - return filePath, nil - - }, -} - diff --git a/tools/vim_linux_amd64.go b/tools/vim_linux_amd64.go deleted file mode 100644 index 2027918..0000000 --- a/tools/vim_linux_amd64.go +++ /dev/null @@ -1,43 +0,0 @@ -//go:build linux && amd64 - -package tools - -import ( - "strings" - "text/template" - - "github.com/mikoto2000/devcontainer.vim/v3/util" -) - -// Vim のダウンロード URL -const vimDownloadURLPattern = "https://github.com/vim/vim-appimage/releases/download/{{ .TagName }}/Vim-{{ .TagName }}.glibc2.29-x86_64.AppImage" - -// Vim のツール情報 -var VIM Tool = Tool{ - FileName: "vim", - CalculateDownloadURL: func() string { - latestTagName, err := util.GetLatestReleaseFromGitHub("vim", "vim-appimage") - if err != nil { - panic(err) - } - - pattern := "pattern" - tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern) - if err != nil { - panic(err) - } - - tmplParams := map[string]string{"TagName": latestTagName} - var downloadURL strings.Builder - err = tmpl.Execute(&downloadURL, tmplParams) - if err != nil { - panic(err) - } - return downloadURL.String() - }, - installFunc: func(downloadURL string, filePath string) (string, error) { - return simpleInstall(downloadURL, filePath) - }, -} - - diff --git a/tools/vim_windows_amd64.go b/tools/vim_windows_amd64.go deleted file mode 100644 index 1164e7c..0000000 --- a/tools/vim_windows_amd64.go +++ /dev/null @@ -1,44 +0,0 @@ -//go:build windows && amd64 - -package tools - -import ( - "strings" - "text/template" - - "github.com/mikoto2000/devcontainer.vim/v3/util" -) - -// Vim のダウンロード URL -const vimDownloadURLPattern = "https://github.com/vim/vim-appimage/releases/download/{{ .TagName }}/Vim-{{ .TagName }}.glibc2.29-x86_64.AppImage" - -// Vim のツール情報 -var VIM Tool = Tool{ - FileName: "vim", - CalculateDownloadURL: func() string { - latestTagName, err := util.GetLatestReleaseFromGitHub("vim", "vim-appimage") - if err != nil { - panic(err) - } - - pattern := "pattern" - tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern) - if err != nil { - panic(err) - } - - tmplParams := map[string]string{"TagName": latestTagName} - var downloadURL strings.Builder - err = tmpl.Execute(&downloadURL, tmplParams) - if err != nil { - panic(err) - } - return downloadURL.String() - }, - installFunc: func(downloadURL string, filePath string) (string, error) { - return simpleInstall(downloadURL, filePath) - }, -} - - - diff --git a/util/util.go b/util/util.go index df5c400..17cfda4 100644 --- a/util/util.go +++ b/util/util.go @@ -3,6 +3,7 @@ package util import ( "crypto/md5" "encoding/hex" + "errors" "io/fs" "os" "os/exec" @@ -237,3 +238,16 @@ func ExtractShellVariables(str string) (string, error) { } return string(extractedStrBytes), nil } + +func NormalizeContainerArch(containerArch string) (string, error) { + if containerArch == "amd64" || containerArch == "x86_64" { + return "amd64", nil + } else if containerArch == "arm64" || containerArch == "aarch64" { + return "aarch64", nil + } else if containerArch == "" { + return "", nil + } else { + return "", errors.New("Unknown Architecture") + } + +}