-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (52 loc) · 1.41 KB
/
Copy pathmain.go
File metadata and controls
65 lines (52 loc) · 1.41 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
package main
import (
"context"
"flag"
"fmt"
"os"
)
const version = "0.1.0"
func main() {
outputFile := flag.String("o", "", "write output to `file` (default: stdout)")
includeAll := flag.Bool("a", false, "include stopped containers when no names are given")
showVersion := flag.Bool("v", false, "print version and exit")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: dexport [flags] [CONTAINER...]\n\n")
fmt.Fprintf(os.Stderr, "Export running Docker containers as a docker-compose YAML file.\n\n")
fmt.Fprintf(os.Stderr, "Arguments:\n")
fmt.Fprintf(os.Stderr, " CONTAINER container name or ID (exports all running containers if omitted)\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
flag.Parse()
if *showVersion {
fmt.Println("dexport", version)
os.Exit(0)
}
ctx := context.Background()
cli, err := newDockerClient()
dieOnErr(err)
defer cli.Close()
containerArgs := flag.Args()
var ids []string
if len(containerArgs) > 0 {
ids = containerArgs
} else {
ids, err = listContainers(ctx, cli, *includeAll)
dieOnErr(err)
}
if len(ids) == 0 {
fmt.Fprintln(os.Stderr, "dexport: no containers found")
os.Exit(0)
}
inspected, err := inspectContainers(ctx, cli, ids)
dieOnErr(err)
cf := convertToComposeFile(inspected)
dieOnErr(writeOutput(*outputFile, cf))
}
func dieOnErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "dexport:", err)
os.Exit(1)
}
}