Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Build
on: [push]

env:
GO_VERSION: "1.21"

jobs:

linting:
Expand All @@ -9,27 +12,27 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v6
with:
fetch-depth: 0

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v9
with:
version: v1.54
version: latest

test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v6
with:
go-version: 1.21
go-version: ${{ env.GO_VERSION }}

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v6
with:
fetch-depth: 0

Expand All @@ -46,33 +49,29 @@ jobs:

steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v6
with:
go-version: 1.21
go-version: ${{ env.GO_VERSION }}

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v6
with:
fetch-depth: 0

- name: Login to Docker Hub
uses: docker/login-action@v1
- name: Login to Docker Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Snapshot
uses: goreleaser/goreleaser-action@v5
uses: goreleaser/goreleaser-action@v7
with:
distribution: goreleaser
version: latest
args: release --snapshot

- name: Release
if: startsWith(github.ref, 'refs/tags/')
uses: goreleaser/goreleaser-action@v5
uses: goreleaser/goreleaser-action@v7
with:
distribution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
47 changes: 47 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: "2"
run:
tests: true
linters:
enable:
- asciicheck
- bidichk
- gocheckcompilerdirectives
- misspell
- rowserrcheck
- sqlclosecheck
- forbidigo
disable:
- gochecknoglobals
- prealloc
- wsl
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
paths:
- third_party$
- builtin$
- examples$
rules:
- path: cmd
linters:
- forbidigo
- path: fmt
linters:
- forbidigo
settings:
forbidigo:
forbid:
- pattern: ^(fmt\.Print(|f|ln))$
formatters:
enable:
- gofmt
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2

builds:
- env:
Expand Down
26 changes: 16 additions & 10 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd

import (
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/gomicro/forge/fmt"
)

const (
Expand All @@ -28,23 +28,29 @@ func init() {
var CompletionCmd = &cobra.Command{
Use: "completion",
Short: "Generate completion files for the forge cli",
Run: completionFunc,
RunE: completionFunc,
}

func completionFunc(cmd *cobra.Command, args []string) error {
return generateCompletion(shell, os.Stdout)
}

func completionFunc(cmd *cobra.Command, args []string) {
func generateCompletion(targetShell string, out io.Writer) error {
var err error
switch strings.ToLower(shell) {
switch strings.ToLower(targetShell) {
case "bash":
err = RootCmd.GenBashCompletion(os.Stdout)
err = RootCmd.GenBashCompletion(out)
case "ps", "powershell", "power_shell":
err = RootCmd.GenPowerShellCompletion(os.Stdout)
err = RootCmd.GenPowerShellCompletion(out)
case "zsh":
err = RootCmd.GenZshCompletion(os.Stdout)
err = RootCmd.GenZshCompletion(out)
default:
return fmt.Errorf("unsupported shell: %q", targetShell)
}

if err != nil {
fmt.Printf("error generating completion output: %v", err.Error())
os.Exit(1)
return fmt.Errorf("generating completion output: %w", err)
}

return nil
}
13 changes: 8 additions & 5 deletions cmd/confmt.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/gomicro/forge/confile"
"github.com/gomicro/forge/fmt"
)

func init() {
Expand All @@ -15,17 +16,19 @@ var confmtCmd = &cobra.Command{
Use: "confmt",
Short: "Format the forge config file",
Long: `Format and adjust the forge file for consistency.`,
Run: confmtFunc,
RunE: confmtFunc,
}

func confmtFunc(cmd *cobra.Command, args []string) {
func confmtFunc(cmd *cobra.Command, args []string) error {
conf, err := confile.ParseFromFile()
if err != nil {
fmt.Printf("Failed: %v", err.Error())
return fmt.Errorf("parsing config file: %w", err)
}

err = conf.Fmt()
if err != nil {
fmt.Printf("Failed: %v", err.Error())
return fmt.Errorf("formatting config file: %w", err)
}

return nil
}
17 changes: 9 additions & 8 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cmd

import (
"errors"
"fmt"
"os"
"path"

"github.com/spf13/cobra"

"github.com/gomicro/forge/confile"
"github.com/gomicro/forge/fmt"
)

func init() {
Expand All @@ -18,19 +19,17 @@ var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a forge config file",
Long: `Collects the basic information to initialize a forge config file.`,
Run: initFunc,
RunE: initFunc,
}

func initFunc(cmd *cobra.Command, args []string) {
func initFunc(cmd *cobra.Command, args []string) error {
if confile.Exists() {
fmt.Printf("config file already exists")
os.Exit(1)
return errors.New("config file already exists")
}

currentDir, err := os.Getwd()
if err != nil {
fmt.Printf("Failed to get current working dir: %v", err.Error())
os.Exit(1)
return fmt.Errorf("getting current working dir: %w", err)
}

projName := path.Base(currentDir)
Expand All @@ -49,6 +48,8 @@ func initFunc(cmd *cobra.Command, args []string) {

err = f.Fmt()
if err != nil {
fmt.Printf("Error Initializing File: %v", err.Error())
return fmt.Errorf("initializing file: %w", err)
}

return nil
}
29 changes: 15 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"fmt"
"os"
"sort"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/gomicro/forge/confile"
"github.com/gomicro/forge/fmt"
clifmt "github.com/gomicro/forge/fmt"
)

func init() {
Expand All @@ -17,28 +18,28 @@ func init() {
RootCmd.PersistentFlags().Bool("verbose", false, "show more verbose output")
err := viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
if err != nil {
fmt.Printf("Error setting up: %v\n", err.Error())
clifmt.Printf("Error setting up: %v\n", err.Error())
os.Exit(1)
}

RootCmd.PersistentFlags().Bool("solo", false, "run a step solo, without its pre or post steps")
err = viper.BindPFlag("solo", RootCmd.PersistentFlags().Lookup("solo"))
if err != nil {
fmt.Printf("Error setting up: %v\n", err.Error())
clifmt.Printf("Error setting up: %v\n", err.Error())
os.Exit(1)
}

RootCmd.PersistentFlags().Bool("no-pre", false, "skip running pre steps")
err = viper.BindPFlag("no-pre", RootCmd.PersistentFlags().Lookup("no-pre"))
if err != nil {
fmt.Printf("Error setting up: %v\n", err.Error())
clifmt.Printf("Error setting up: %v\n", err.Error())
os.Exit(1)
}

RootCmd.PersistentFlags().Bool("no-post", false, "skip running post steps")
err = viper.BindPFlag("no-post", RootCmd.PersistentFlags().Lookup("no-post"))
if err != nil {
fmt.Printf("Error setting up: %v\n", err.Error())
clifmt.Printf("Error setting up: %v\n", err.Error())
os.Exit(1)
}
}
Expand All @@ -52,41 +53,41 @@ var RootCmd = &cobra.Command{
Short: "A CLI for building projects",
Long: `Forge is a CLI tool for executing, in a consistent manner, scripts and commands for building and maintaining projects.`,
Args: cobra.MinimumNArgs(1),
Run: rootFunc,
RunE: rootFunc,
SilenceErrors: true,
ValidArgsFunction: validArgsFunc,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Printf("Failed to execute: %v", err.Error())
clifmt.Printf("Failed to execute: %v\n", err.Error())
os.Exit(1)
}
}

func rootFunc(cmd *cobra.Command, args []string) {
func rootFunc(cmd *cobra.Command, args []string) error {
conf, err := confile.ParseFromFile()
if err != nil {
fmt.Printf("Failed: %v", err.Error())
os.Exit(1)
return err
}

for _, a := range args {
_, found := conf.Steps[a]
if !found {
fmt.Printf("target not found: %v", a)
os.Exit(1)
return fmt.Errorf("target not found: %v", a)
}
}

for _, s := range args {
err := conf.Steps[s].Execute(conf.Steps, conf.Envs, conf.Vars)
if err != nil {
fmt.Printf("failed executing step %v: %v", s, err.Error())
os.Exit(1)
return fmt.Errorf("executing step %v: %w", s, err)
}
}

return nil
}

func validArgsFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
Loading
Loading