-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (92 loc) · 2.45 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli"
)
func main() {
app := &cli.App{
Name: "Docker 2 WSL",
Usage: "Convert a Docker image to a WSL distribution",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "distro-name",
Usage: "the name of the WSL distribution",
Value: "dist",
},
&cli.StringFlag{
Name: "image",
Usage: "The name of the image or 'Dockerfile' if you want to build the Dockerfile in your current directory",
Value: "Dockerfile",
},
&cli.BoolFlag{
Name: "launch",
Usage: "Launch the WSL distribution after importing",
},
&cli.BoolFlag{
Name: "set-default",
Usage: "Set the distribution as the default",
},
&cli.BoolFlag{
Name: "start-menu",
Usage: "Add the distribution to the Start Menu",
},
},
Action: func(c *cli.Context) error {
var ImageName = c.String("image")
var distroName = c.String("distro-name")
_, err := os.Stat(ImageName)
hasDockerfile := err == nil
var img *string
if !hasDockerfile {
println("Pulling Docker image...")
img, err = pullDockerImage(ImageName)
if err != nil {
return fmt.Errorf("failed to pull Docker image: %v", err)
}
} else {
println("Building Docker image...")
img, err = buildDocker(ImageName)
if err != nil {
return fmt.Errorf("failed to build Docker image: %v", err)
}
}
fmt.Printf("Successfully built Docker image '%s'\n", *img)
err = exportDockerImage(*img)
if err != nil {
return fmt.Errorf("failed to export Docker image: %v", err)
}
defer os.Remove("image.tar")
err = importWsl(distroName)
if err != nil {
return fmt.Errorf("failed to import WSL: %v", err)
}
fmt.Printf("Successfully imported %s to WSL\n", *img)
fmt.Printf("Run `wsl -d %s` to launch the distribution\n", distroName)
fmt.Printf("Run `wsl --unregister %s` to remove the distribution and files\n", distroName)
if c.Bool("set-default") {
err = setDefaultWsl(distroName)
if err != nil {
fmt.Printf("failed to set distro as default: %v\n", err)
}
}
if c.Bool("start-menu") {
err = addToStartMenu(distroName)
if err != nil {
fmt.Printf("failed to add to Start Menu: %v\n", err)
}
}
if c.Bool("launch") {
err = launchWsl(distroName)
if err != nil {
return fmt.Errorf("failed to launch WSL: %v", err)
}
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}