Skip to content

Commit

Permalink
Merge pull request #28 from mikoto2000/add-self-update
Browse files Browse the repository at this point in the history
Add self-update command to devcontainer.vim
  • Loading branch information
mikoto2000 authored Oct 26, 2024
2 parents a7dbfdd + 7959498 commit ae68d80
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ VERSION:
1.0.11
COMMANDS:
run Run container use `docker run`
templates Run `devcontainer templates`
start Run `devcontainer up` and `devcontainer exec`
stop Stop devcontainers.
down Stop and remove devcontainers.
config devcontainer.vim's config information.
vimrc devcontainer.vim's vimrc information.
runargs run subcommand's default arguments.
tool Management tools
clean clean workspace cache files.
index Management index file
help, h Shows a list of commands or help for one command
run Run container use `docker run`
templates Run `devcontainer templates`
start Run `devcontainer up` and `devcontainer exec`
stop Stop devcontainers.
down Stop and remove devcontainers.
config devcontainer.vim's config information.
vimrc devcontainer.vim's vimrc information.
runargs run subcommand's default arguments.
tool Management tools
clean clean workspace cache files.
index Management index file
self-update Update devcontainer.vim itself
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--license, -l show licensesa.
Expand Down Expand Up @@ -105,6 +106,13 @@ devcontainer.vim tool vim download
devcontainer.vim tool devcontainer download
```

#### devcontainer.vim 自身のアップデート

`self-update` サブコマンドを使用して、 `devcontainer.vim` 自身を最新バージョンに更新できます。

```sh
devcontainer.vim self-update
```

### テンプレートをもとに `devcontainer.json` を作成する

Expand Down
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func main() {
// vimrc ファイルの出力先を組み立て
// vimrc を出力(既に存在するなら何もしない)
vimrc := filepath.Join(appConfigDir, "vimrc")
if !util.IsExists(vimrc) {
if (!util.IsExists(vimrc)) {
err := util.CreateFileWithContents(vimrc, additionalVimrc, 0666)
if err != nil {
panic(err)
Expand All @@ -91,7 +91,7 @@ func main() {
// runargs ファイルの出力先を組み立て
// runargs を出力(既に存在するなら何もしない)
runargs := filepath.Join(appConfigDir, "runargs")
if !util.IsExists(runargs) {
if (!util.IsExists(runargs)) {
err := util.CreateFileWithContents(runargs, runargsContent, 0666)
if err != nil {
panic(err)
Expand Down Expand Up @@ -193,7 +193,7 @@ func main() {
// Features の一覧をダウンロード
indexFileName := "devcontainer-index.json"
indexFile := filepath.Join(appCacheDir, indexFileName)
if !util.IsExists(indexFile) {
if (!util.IsExists(indexFile)) {
fmt.Println("Download template index ... ")
oras.Pull("ghcr.io/devcontainers/index", "latest", appCacheDir)
fmt.Println("done.")
Expand Down Expand Up @@ -634,6 +634,18 @@ func main() {
},
},
},
{
Name: "self-update",
Usage: "Update devcontainer.vim itself",
UsageText: "devcontainer.vim self-update",
Action: func(cCtx *cli.Context) error {
err := tools.SelfUpdate()
if err != nil {
panic(err)
}
return nil
},
},
},
})

Expand Down
46 changes: 46 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"

Expand Down Expand Up @@ -194,3 +195,48 @@ func InstallDownTools(installDir string) (string, error) {
devcontainerPath, err := DEVCONTAINER.Install(installDir, false)
return devcontainerPath, err
}

// SelfUpdate downloads the latest release of devcontainer.vim from GitHub and replaces the current binary
func SelfUpdate() error {
// Get the latest release tag name from GitHub
latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "devcontainer.vim")
if err != nil {
return err
}

// Construct the download URL for the latest release
var downloadURL string
if runtime.GOOS == "windows" {
downloadURL = fmt.Sprintf("https://github.com/mikoto2000/devcontainer.vim/releases/download/%s/devcontainer.vim-windows-amd64.exe", latestTagName)
} else if runtime.GOOS == "darwin" {
downloadURL = fmt.Sprintf("https://github.com/mikoto2000/devcontainer.vim/releases/download/%s/devcontainer.vim-darwin-amd64", latestTagName)
} else {
downloadURL = fmt.Sprintf("https://github.com/mikoto2000/devcontainer.vim/releases/download/%s/devcontainer.vim-linux-amd64", latestTagName)
}

// Download the latest release
executablePath, err := os.Executable()
if err != nil {
return err
}

// Rename the current binary to avoid "text file busy" error
tempPath := executablePath + ".old"
err = os.Rename(executablePath, tempPath)
if err != nil {
return err
}

_, err = simpleInstall(downloadURL, executablePath)
if err != nil {
// Restore the original binary if download fails
os.Rename(tempPath, executablePath)
return err
}

// Remove the old binary
os.Remove(tempPath)

fmt.Println("devcontainer.vim has been updated to the latest version.")
return nil
}

0 comments on commit ae68d80

Please sign in to comment.