Skip to content

Commit

Permalink
feature: list && update cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
李昌 committed Dec 15, 2024
1 parent 52dd5d7 commit 8263478
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/.goreleaser-for-darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ builds:
- amd64
- arm64
checksum:
disable: true
disable: true
1 change: 1 addition & 0 deletions .github/workflows/.goreleaser-for-linux.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
before:
hooks:
- go mod tidy

builds:
- id: pm
main: .
Expand Down
21 changes: 0 additions & 21 deletions .github/workflows/.goreleaser-for-windows.yml

This file was deleted.

81 changes: 19 additions & 62 deletions .github/workflows/release_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ jobs:
- name: Install cross-compiler for linux/arm64
run: sudo apt-get install -y gcc-aarch64-linux-gnu

- name: Set up git tag
uses: devops-actions/[email protected]
with:
strip_v: false

- name: Replace __VERSION__ in cmds/root.go
run: sed -i "s/__VERSION__/${{ steps.get_tag.outputs.tag }}/" cmds/root.go

- name: Set up Go
uses: actions/setup-go@v4

Expand All @@ -28,7 +36,7 @@ jobs:
with:
distribution: goreleaser
version: latest
args: release --config .github/workflows/.goreleaser-for-linux.yml
args: release --skip=validate --config .github/workflows/.goreleaser-for-linux.yml
env:
GITHUB_TOKEN: ${{ secrets.GO_BUILD_TOKEN}}

Expand All @@ -48,48 +56,25 @@ jobs:

- name: Set up Go
uses: actions/setup-go@v4

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --config .github/workflows/.goreleaser-for-darwin.yml
env:
GITHUB_TOKEN: ${{ secrets.GO_BUILD_TOKEN}}

- name: Upload assets
uses: actions/upload-artifact@v3
with:
name: myapp
path: dist/*

release-windows-binary:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up MinGW
uses: egor-tensin/setup-mingw@v2
go-version: 1.23.1 # 指定 Go 版本
- name: Set up git tag
uses: devops-actions/[email protected]
with:
platform: x64
version: 12.2.0

- name: Set up Go
uses: actions/setup-go@v4
strip_v: false

- name: Replace __VERSION__ in cmds/root.go
run: sed -i '' "s/__VERSION__/${{ steps.get_tag.outputs.tag }}/" cmds/root.go

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --config .github/workflows/.goreleaser-for-windows.yml
args: release --skip=validate --config .github/workflows/.goreleaser-for-darwin.yml
env:
CGO_ENABLE: 1
GITHUB_TOKEN: ${{ secrets.GO_BUILD_TOKEN}}

- name: Upload assets
Expand All @@ -98,31 +83,3 @@ jobs:
name: myapp
path: dist/*

# goreleaser:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# with:
# fetch-depth: 0

# - name: Set up Go
# uses: actions/setup-go@v4

# - name: Run GoReleaser
# uses: goreleaser/goreleaser-action@v5
# with:
# # either 'goreleaser' (default) or 'goreleaser-pro'
# distribution: goreleaser
# version: latest
# args: release --clean
# env:
# GITHUB_TOKEN: ${{ secrets.GO_BUILD_TOKEN}}
# # Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
# # GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}

# - name: Upload assets
# uses: actions/upload-artifact@v3
# with:
# name: myapp
# path: myfolder/dist/*
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ ifeq ($(origin GOBIN),undefined)
GOBIN := $(GOPATH)/bin
endif

# 获取当前的 Git 提交哈希
GIT_COMMIT := $(shell git rev-parse HEAD)

# 获取当前的 Git 标签(如果有)
GIT_TAG := $(shell git tag --points-at HEAD 2>/dev/null)

# 如果没有标签,则使用提交哈希作为版本号
VERSION := $(if $(GIT_TAG),$(GIT_TAG),$(GIT_COMMIT))

## go.build.linux_amd64.<service>
.PHONY: build.%
build.%:
Expand All @@ -30,6 +39,7 @@ build.%:
$(eval OS := $(word 1,$(subst _, ,$(PLATFORM))))
$(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM))))
@echo "==========> Building binary $(COMMAND) for $(GOOS) $(GOARCH)"
@sed -i 's/__VERSION__/$(VERSION)/' cmds/root.go
@GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/$(COMMAND)$(GO_OUT_EXT) main.go

## build: 编译所有服务为二进制可执行文件
Expand Down
7 changes: 6 additions & 1 deletion cmds/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ func DelCmd() *cobra.Command {
os.Exit(1)
}

nameList, err := service.store.SearchName(cmd.Context(), toComplete)
passwdList, err := service.store.SearchName(cmd.Context(), toComplete)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

nameList := make([]string, 0)
for _, passed := range passwdList {
nameList = append(nameList, passed.Name)
}
return nameList, cobra.ShellCompDirectiveKeepOrder
},
}
Expand Down
7 changes: 6 additions & 1 deletion cmds/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ func GetCmd() *cobra.Command {
os.Exit(1)
}

nameList, err := service.store.SearchName(cmd.Context(), toComplete)
passwdList, err := service.store.SearchName(cmd.Context(), toComplete)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

nameList := make([]string, 0)
for _, passed := range passwdList {
nameList = append(nameList, passed.Name)
}
return nameList, cobra.ShellCompDirectiveKeepOrder
},
}
Expand Down
1 change: 1 addition & 0 deletions cmds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func InitCmd() *cobra.Command {
"storeConfig": storeConfigStr,
"localPath": filepath.Join(home, ".pm/store"),
"userKeyPath": userKeyPath,
"latency": "24h",
}
tmpl, err := template.New("conf").Parse(confTmpl)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions cmds/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmds

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/yangchnet/pm/config"
)

func ListCmd() *cobra.Command {
var (
filterString string
)
var listCmd = &cobra.Command{
Use: "list [-f <filter_string>]",
Short: "list passwd name",
PreRun: func(cmd *cobra.Command, args []string) {
config.InitConfig()

service, err := NewService(cmd.Context())
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := service.remote.Pull(cmd.Context()); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
service, err := NewService(cmd.Context())
if err != nil {
fmt.Println(err)
os.Exit(1)
}

passwds, err := service.store.SearchName(cmd.Context(), filterString)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Printf("%18s| %18s| %18s| %18s|\n", "name", "account", "note", "url")
fmt.Printf("%18s| %18s| %18s| %18s|\n", "-----", "-----", "-----", "-----")
for _, passwd := range passwds {
fmt.Printf("%18s| %18s| %18s| %18s|\n", passwd.Name, passwd.UserName, passwd.Note, passwd.Url)
}

},
}

listCmd.Flags().StringVarP(&filterString, "filter", "f", "", "passwd filter string, default \"\"")

return listCmd
}
7 changes: 6 additions & 1 deletion cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmds

import "github.com/spf13/cobra"

var RootCmd = cobra.Command{Use: "pm"}
var RootCmd = cobra.Command{
Use: "pm",
Version: "__VERSION__",
}

func init() {
RootCmd.AddCommand(GenerateCmd())
Expand All @@ -11,4 +14,6 @@ func init() {
RootCmd.AddCommand(PullCmd())
RootCmd.AddCommand(InitCmd())
RootCmd.AddCommand(DelCmd())
RootCmd.AddCommand(ListCmd())
RootCmd.AddCommand(UpdateCmd())
}
96 changes: 96 additions & 0 deletions cmds/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmds

import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/yangchnet/pm/config"
"github.com/yangchnet/pm/store"
"gorm.io/gorm"
)

func UpdateCmd() *cobra.Command {
var (
username string
password string
url string
note string
)
var updateCmd = &cobra.Command{
Use: "update <password> [-u username -p password -l url -n note]",
Short: "update passwd",
PreRun: func(cmd *cobra.Command, args []string) {
config.InitConfig()

service, err := NewService(cmd.Context())
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := service.remote.Pull(cmd.Context()); err != nil {
fmt.Println(err)
os.Exit(1)
}

if len(args) < 1 {
fmt.Println("必须执行密码名称!")
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
service, err := NewService(cmd.Context())
if err != nil {
fmt.Println(err)
os.Exit(1)
}

passwd, err := service.store.Get(cmd.Context(), args[0])
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
fmt.Printf("不存在的密码 [%s]", args[0])
os.Exit(1)
}
os.Exit(0)
}

if password != "" {
primaryKey := GetPrimaryKey()
passwd.CryptedPasswd, err = store.Encrypt(primaryKey, []byte(password))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

if note != "" {
passwd.Note = note
}

if url != "" {
passwd.Url = url
}

if username != "" {
passwd.UserName = username
}

fmt.Printf("note: %s; url: %s", note, url)

err = service.store.Update(cmd.Context(), args[0], passwd)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

updateCmd.Flags().StringVarP(&username, "username", "u", "", "passwd username")
updateCmd.Flags().StringVarP(&url, "url", "l", "", "passwd url")
updateCmd.Flags().StringVarP(&note, "note", "n", "", "passwd note")
updateCmd.Flags().StringVarP(&password, "password", "p", "", "raw passwd")

return updateCmd
}
Loading

0 comments on commit 8263478

Please sign in to comment.