Skip to content

Commit

Permalink
feat(copier): read module name from go mod edit (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored Sep 28, 2023
1 parent 24e6853 commit 5094bb4
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions cmd/copier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,35 @@
package main

import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"

"go-simpler.org/assert"
)

func main() {
if err := run(); err != nil {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

if err := run(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func run() error {
func run(ctx context.Context) error {
if len(os.Args) < 2 {
return errors.New("path to package is not specified")
}

moduleName, err := readModuleName()
moduleName, err := readModuleName(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -66,20 +72,28 @@ func run() error {
return nil
}

func readModuleName() (string, error) {
f, err := os.Open("go.mod")
func readModuleName(ctx context.Context) (string, error) {
args := [...]string{"go", "mod", "edit", "-json"}

out, err := exec.CommandContext(ctx, args[0], args[1:]...).Output()
if err != nil {
return "", err
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return "", fmt.Errorf("running %q: %w: %s", strings.Join(args[:], " "), err, string(exitErr.Stderr))
}
return "", fmt.Errorf("running %q: %w", strings.Join(args[:], " "), err)
}
defer f.Close()

header, err := bufio.NewReader(f).ReadString('\n')
if err != nil {
return "", err
var info struct {
Module struct {
Path string `json:"Path"`
} `json:"Module"`
}
if err := json.Unmarshal(out, &info); err != nil {
return "", fmt.Errorf("decoding module info: %w", err)
}

name := strings.TrimPrefix(header, "module")
return strings.TrimSpace(name), nil
return info.Module.Path, nil
}

func writeFile(name, content string) error {
Expand Down

0 comments on commit 5094bb4

Please sign in to comment.