generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathboxomigrate.go
More file actions
164 lines (145 loc) · 3.89 KB
/
boxomigrate.go
File metadata and controls
164 lines (145 loc) · 3.89 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"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",
},
&cli.BoolFlag{
Name: "force",
Usage: "run even if no .git folder is found",
},
},
Action: func(clictx *cli.Context) error {
dryrun := clictx.Bool("dryrun")
force := clictx.Bool("force")
configFile := clictx.String("config")
migrator, err := buildMigrator(dryrun, configFile)
if err != nil {
return err
}
fmt.Printf("\n\n")
if !force {
p, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to fetch current working directory: %w", err)
}
for {
g := filepath.Join(p, ".git")
_, err := os.Stat(g)
if err == nil {
break
}
newP := filepath.Dir(p)
if p == newP {
return fmt.Errorf(`
⚠️ Version Control System Check ⚠️
We couldn't locate a .git folder in any parent paths. We strongly recommend
using a Version Control System to help you easily compare and revert to a
previous state if needed, as this tool doesn't have an undo feature.
If you're using a different VCS or like to live dangerously, you can bypass this
check by adding the --force flag.`)
}
p = newP
}
}
if !dryrun {
err := migrator.GoGet("github.com/ipfs/boxo@v0.8.0")
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)
}
}