@@ -2,19 +2,23 @@ package cmd
22
33import (
44 "context"
5+ "fmt"
56 "io"
7+ "log"
68 "os"
79
810 "github.com/docker/docker/api/types/container"
911 "github.com/docker/docker/api/types/image"
1012 "github.com/docker/docker/client"
1113 "github.com/docker/go-connections/nat"
14+ "github.com/microcks/microcks-cli/pkg/config"
1215 "github.com/spf13/cobra"
1316)
1417
1518func NewStartCommand () * cobra.Command {
1619 var (
17- hostIP string
20+ hostIP string = "0.0.0.0"
21+ name string
1822 hostPort string
1923 imageName string
2024 autoRemove bool
@@ -28,22 +32,62 @@ microcks start
2832# Define your port (by default 8585)
2933microcks start --port [Port you want]` ,
3034 Run : func (cmd * cobra.Command , args []string ) {
31- startContainer (imageName , hostIP , hostPort , autoRemove )
35+ cfg , err := config .EnsureConfig (config .ConfigPath )
36+
37+ if err != nil {
38+ log .Fatalf ("Error loading config: %v" , err )
39+ }
40+
41+ if cfg .Instance .Status == "Running" {
42+ fmt .Println ("Microcks is already running." )
43+ return
44+ }
45+
46+ if cfg .Instance .Status == "Stopped" {
47+ if err := startContainer (cfg .Instance .ContainerID ); err != nil {
48+ fmt .Errorf ("failed to start container: %v" , err )
49+ }
50+ fmt .Println ("Microcks started successfully..." )
51+ return
52+ }
53+
54+ cfg .Instance .Name = name
55+ cfg .Instance .Image = imageName
56+ cfg .Instance .Port = hostPort
57+ cfg .Instance .AutoRemove = autoRemove
58+
59+ containerID , err := createContainer (cfg , hostIP )
60+
61+ if err != nil {
62+ log .Fatalf ("Failed to create a container: %v" , err )
63+ }
64+ cfg .Instance .ContainerID = containerID
65+
66+ if err := startContainer (cfg .Instance .ContainerID ); err != nil {
67+ fmt .Errorf ("failed to start container: %v" , err )
68+ }
69+
70+ err = config .SaveConfig (config .ConfigPath , cfg )
71+
72+ if err != nil {
73+ log .Fatalf ("Failed to save config: %v" , err )
74+ }
75+
76+ fmt .Printf ("Microcks started successfully..." )
3277 },
3378 }
34-
35- startCmd .Flags ().StringVar (& hostIP , "IP" , "localhost" , "" )
79+ startCmd .Flags ().StringVar (& name , "name" , "microcks" , "name for you microcks instance" )
3680 startCmd .Flags ().StringVar (& hostPort , "port" , "8585" , "" )
3781 startCmd .Flags ().StringVar (& imageName , "image" , "quay.io/microcks/microcks-uber:latest-native" , "image which will be used to create a container" )
3882 startCmd .Flags ().BoolVar (& autoRemove , "rm" , false , "mimic of '--rm' flag of dokcer to automatically remove the container when it exits" )
3983 return startCmd
4084}
4185
42- func startContainer ( imageName string , hostIP string , hostPort string , autoRemove bool ) {
86+ func createContainer ( cfg * config. Config , hostIP string ) ( string , error ) {
4387 ctx := context .Background ()
4488 cli , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
4589 if err != nil {
46- panic ( err )
90+ return "" , err
4791 }
4892 defer cli .Close ()
4993
@@ -53,34 +97,47 @@ func startContainer(imageName string, hostIP string, hostPort string, autoRemove
5397 exposedPort : []nat.PortBinding {
5498 {
5599 HostIP : hostIP ,
56- HostPort : hostPort ,
100+ HostPort : cfg . Instance . Port ,
57101 },
58102 },
59103 }
60104
61- out , err := cli .ImagePull (ctx , imageName , image.PullOptions {})
105+ out , err := cli .ImagePull (ctx , cfg . Instance . Image , image.PullOptions {})
62106 if err != nil {
63- panic ( err )
107+ return "" , err
64108 }
65109 defer out .Close ()
66110 io .Copy (os .Stdout , out )
67111
68112 resp , err := cli .ContainerCreate (
69113 ctx ,
70114 & container.Config {
71- Image : imageName ,
115+ Image : cfg . Instance . Image ,
72116 ExposedPorts : nat.PortSet {exposedPort : struct {}{}},
73117 },
74118 & container.HostConfig {
75119 PortBindings : portBindings ,
76- AutoRemove : autoRemove ,
77- }, nil , nil , "" )
120+ AutoRemove : cfg . Instance . AutoRemove ,
121+ }, nil , nil , cfg . Instance . Name )
78122
79123 if err != nil {
80- panic ( err )
124+ return "" , err
81125 }
82126
83- if err := cli .ContainerStart (ctx , resp .ID , container.StartOptions {}); err != nil {
127+ return resp .ID , nil
128+ }
129+
130+ func startContainer (cotainerID string ) error {
131+ ctx := context .Background ()
132+ cli , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
133+ if err != nil {
134+ return err
135+ }
136+ defer cli .Close ()
137+
138+ if err := cli .ContainerStart (ctx , cotainerID , container.StartOptions {}); err != nil {
84139 panic (err )
85140 }
141+
142+ return nil
86143}
0 commit comments