/
main.go
103 lines (85 loc) 路 2.44 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
package main
import (
"errors"
"fmt"
"os"
color "github.com/fatih/color"
registry "github.com/miguelmota/ipdr/registry"
cobra "github.com/spf13/cobra"
)
var green = color.New(color.FgGreen)
var (
// ErrImageIDRequired is error for when image ID is required
ErrImageIDRequired = errors.New("image hash or name is required")
// ErrOnlyOneArgumentRequired is error for when one argument only is required
ErrOnlyOneArgumentRequired = errors.New("only one argument is required")
)
func main() {
var ipfsHost string
rootCmd := &cobra.Command{
Use: "ipdr",
Short: "InterPlanetary Docker Registry",
Long: `The command-line interface for the InterPlanetary Docker Registry.
More info: https://github.com/miguelmota/ipdr`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
pushCmd := &cobra.Command{
Use: "push",
Short: "Push image to IPFS-backed Docker registry",
Long: "Push the Docker image to the InterPlanetary Docker Registry hosted on IPFS",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return ErrImageIDRequired
}
if len(args) != 1 {
return ErrOnlyOneArgumentRequired
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
reg := registry.NewRegistry(®istry.Config{
DockerLocalRegistryHost: "5000",
IPFSHost: ipfsHost,
})
imageID := args[0]
hash, err := reg.PushImageByID(imageID)
if err != nil {
return err
}
fmt.Println(green.Sprintf("\nSuccessfully pushed Docker image to IPFS:\n/ipfs/%s", hash))
return nil
},
}
pushCmd.Flags().StringVarP(&ipfsHost, "ipfs-host", "", "127.0.0.1:5001", "A remote IPFS API host to push the image to. Example: 127.0.0.1:5001")
pullCmd := &cobra.Command{
Use: "pull",
Short: "Pull image from the IPFS-backed Docker registry",
Long: "Pull the Docker image from the InterPlanetary Docker Registry hosted on IPFS",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return ErrImageIDRequired
}
if len(args) != 1 {
return ErrOnlyOneArgumentRequired
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
reg := registry.NewRegistry(®istry.Config{
DockerLocalRegistryHost: "docker.localhost",
})
imageHash := args[0]
_, err := reg.PullImage(imageHash)
return err
},
}
rootCmd.AddCommand(
pushCmd,
pullCmd,
)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}