forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.go
112 lines (94 loc) · 2.1 KB
/
mod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/gnolang/gno/pkgs/commands"
"github.com/gnolang/gno/pkgs/errors"
"github.com/gnolang/gno/pkgs/gnolang/gnomod"
)
type modDownloadCfg struct {
remote string
}
func newModCmd(io *commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
Name: "mod",
ShortUsage: "mod <command>",
ShortHelp: "Manage gno.mod",
},
commands.NewEmptyConfig(),
commands.HelpExec,
)
cmd.AddSubCommands(
newModDownloadCmd(io),
)
return cmd
}
func newModDownloadCmd(io *commands.IO) *commands.Command {
cfg := &modDownloadCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "download",
ShortUsage: "download [flags]",
ShortHelp: "Download modules to local cache",
},
cfg,
func(_ context.Context, args []string) error {
return execModDownload(cfg, args, io)
},
)
}
func (c *modDownloadCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.remote,
"remote",
"test3.gno.land:36657",
"remote for fetching gno modules",
)
}
func execModDownload(cfg *modDownloadCfg, args []string, io *commands.IO) error {
if len(args) > 0 {
return flag.ErrHelp
}
path, err := os.Getwd()
if err != nil {
return err
}
modPath := filepath.Join(path, "gno.mod")
if !isFileExist(modPath) {
return errors.New("gno.mod not found")
}
// read gno.mod
data, err := os.ReadFile(modPath)
if err != nil {
return fmt.Errorf("readfile %q: %w", modPath, err)
}
// parse gno.mod
gnoMod, err := gnomod.Parse(modPath, data)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
// sanitize gno.mod
gnoMod.Sanitize()
// validate gno.mod
if err := gnoMod.Validate(); err != nil {
return fmt.Errorf("validate: %w", err)
}
// fetch dependencies
if err := gnoMod.FetchDeps(cfg.remote); err != nil {
return fmt.Errorf("fetch: %w", err)
}
gomod, err := gnomod.GnoToGoMod(*gnoMod)
if err != nil {
return fmt.Errorf("sanitize: %w", err)
}
// write go.mod file
err = gomod.WriteToPath(path)
if err != nil {
return fmt.Errorf("write go.mod file: %w", err)
}
return nil
}