generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 98
/
boxomigrate.go
130 lines (115 loc) · 2.98 KB
/
boxomigrate.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"log"
"os"
"strings"
migrate "github.com/ipfs/boxo/cmd/boxo-migrate/internal"
"github.com/urfave/cli/v2"
)
func loadConfig(configFile string) (migrate.Config, error) {
if configFile != "" {
f, err := os.Open(configFile)
if err != nil {
return migrate.Config{}, fmt.Errorf("opening config file: %w", err)
}
defer f.Close()
return migrate.ReadConfig(f)
}
return migrate.DefaultConfig, nil
}
func buildMigrator(dryrun bool, configFile string) (*migrate.Migrator, error) {
config, err := loadConfig(configFile)
if err != nil {
return nil, err
}
dir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("getting working dir: %w", err)
}
return &migrate.Migrator{
DryRun: dryrun,
Dir: dir,
Config: config,
}, nil
}
func main() {
app := &cli.App{
Name: "migrate",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "a JSON config file",
},
},
Commands: []*cli.Command{
{
Name: "update-imports",
Usage: "rewrites imports of the current module for go-libipfs repos",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dryrun",
},
},
Action: func(clictx *cli.Context) error {
dryrun := clictx.Bool("dryrun")
configFile := clictx.String("config")
migrator, err := buildMigrator(dryrun, configFile)
if err != nil {
return err
}
fmt.Printf("\n\n")
if !dryrun {
err := migrator.GoGet("github.com/ipfs/[email protected]")
if err != nil {
return err
}
}
if err := migrator.UpdateImports(); err != nil {
return err
}
if dryrun {
return nil
}
if err := migrator.GoModTidy(); err != nil {
return err
}
fmt.Printf("Your code has been successfully updated. Note that you might still need to manually fix up parts of your code.\n\n")
fmt.Printf("You should also consider running the 'boxo-migrate check-dependencies' command to see if you have any other dependencies on migrated code.\n\n")
return nil
},
},
{
Name: "check-dependencies",
Usage: "checks the current module for dependencies that have migrated to go-libipfs",
Action: func(clictx *cli.Context) error {
configFile := clictx.String("config")
migrator, err := buildMigrator(false, configFile)
if err != nil {
return err
}
deps, err := migrator.FindMigratedDependencies()
if err != nil {
return err
}
if len(deps) > 0 {
fmt.Println(strings.Join([]string{
"You still have dependencies on repos which have migrated to Boxo.",
"You should consider not having these dependencies to avoid multiple versions of the same code.",
"You can use 'go mod why' or 'go mod graph' to find the reason for these dependencies.",
"",
"Dependent module versions:",
"",
strings.Join(deps, "\n"),
}, "\n"))
}
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}