Skip to content

Commit

Permalink
[Fea] support self upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
PWZER committed Nov 19, 2024
1 parent 0e9102a commit ad17ef6
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 409 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Build
on:
workflow_dispatch:
push:
paths-ignore:
- "README.md"
# branches:
# - main
tags:
- "v*"
pull_request_target:
branches:
- main
- dev
jobs:
build:
permissions: write-all
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04]
fail-fast: false
defaults:
run:
shell: bash
working-directory: .
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Set variables
if: ${{github.ref_name=='main'}}
run: echo "VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

- name: Set variables
if: ${{github.ref_name=='' || github.ref_type=='tag'}}
run: echo "VERSION=$(git describe --tags)" >> $GITHUB_ENV

- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.23.0"
check-latest: true

- name: Install UPX
uses: crazy-max/ghaction-upx@v3
with:
install-only: true

- name: Build dssh
run: make all

- name: Compress binaries
run: make compress

- name: Upload Release
if: ${{ success() && github.ref_type=='tag' }}
uses: softprops/action-gh-release@v1
with:
tag: ${{ github.ref_name }}
tag_name: ${{ github.ref_name }}
files: bin/dssh-*
generate_release_notes: true
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
VERSION := $(shell git describe --tags --always)
GIT_COMMIT := $(shell git rev-parse --short HEAD)

GO_BUILD_COMMAND := go build -a -ldflags '-s -w -extldflags "-static" -X "github.com/PWZER/dssh/cmd.Version=$(VERSION)" -X "github.com/PWZER/dssh/cmd.GitCommit=$(GIT_COMMIT)"'

all: darwin linux

compress:
upx --best bin/* || true

darwin-amd64:
GOOS=darwin GOARCH=amd64 $(GO_BUILD_COMMAND) -o bin/dssh-darwin-amd64 .

darwin-arm64:
GOOS=darwin GOARCH=arm64 $(GO_BUILD_COMMAND) -o bin/dssh-darwin-arm64 .

darwin: darwin-amd64 darwin-arm64

linux-amd64:
GOOS=linux GOARCH=amd64 $(GO_BUILD_COMMAND) -o bin/dssh-linux-amd64 .

linux-arm64:
GOOS=linux GOARCH=arm64 $(GO_BUILD_COMMAND) -o bin/dssh-linux-arm64 .

linux: linux-amd64 linux-arm64

install:
GOOS=$(shell uname | tr '[:upper:]' '[:lower:]') GOARCH=$(shell go env GOARCH) $(GO_BUILD_COMMAND) -o ~/.local/bin/dssh .
20 changes: 0 additions & 20 deletions build.sh

This file was deleted.

27 changes: 27 additions & 0 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright © 2024 PWZER <pwzergo@gmail.com>
*/
package cmd

import (
"github.com/PWZER/dssh/utils"
"github.com/spf13/cobra"
)

var (
dummy bool
)

var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "upgrade dssh to the latest version",
Long: "upgrade dssh to the latest version",
RunE: func(cmd *cobra.Command, args []string) error {
return utils.Upgrade(dummy, Version)
},
}

func init() {
rootCmd.AddCommand(upgradeCmd)
upgradeCmd.Flags().BoolVar(&dummy, "dummy", false, "dummy flag for testing")
}
23 changes: 23 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var Version = "v0.0.1"
var GitCommit = "<unknown>"

var versionCmd = &cobra.Command{
Use: "version",
Short: "print dssh version",
Long: "print dssh version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("dssh version: %s, git commit: %s\n", Version, GitCommit)
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
27 changes: 23 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
module github.com/PWZER/dssh

go 1.15
go 1.20

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/sftp v1.13.5
github.com/schollz/progressbar/v3 v3.8.6
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0
github.com/subosito/gotenv v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit ad17ef6

Please sign in to comment.