@@ -36,10 +36,24 @@ func Up(filePath string) (string, error) {
3636 return cmd .NewCommandMgr (cmd .WithTimeout (20 * time .Minute )).RunWithStdoutBashCf ("%s %s up -d" , global .CONF .DockerConfig .Command , loadFiles (filePath ))
3737}
3838
39- func UpWithTask (filePath string , task * task.Task , pullImages bool ) error {
40- if ! pullImages {
41- return cmd .NewCommandMgr (cmd .WithTask (* task )).RunBashCf ("%s %s up -d" , global .CONF .DockerConfig .Command , loadFiles (filePath ))
39+ func UpWithForcePull (filePath string , forcePull bool ) (string , error ) {
40+ if err := checkCmd (); err != nil {
41+ return "" , err
42+ }
43+ if err := pullComposeImages (filePath , forcePull , nil ); err != nil {
44+ return "" , err
45+ }
46+ return cmd .NewCommandMgr (cmd .WithTimeout (20 * time .Minute )).RunWithStdoutBashCf ("%s %s up -d" , global .CONF .DockerConfig .Command , loadFiles (filePath ))
47+ }
48+
49+ func UpWithTask (filePath string , task * task.Task , forcePull bool ) error {
50+ if err := pullComposeImages (filePath , forcePull , task ); err != nil {
51+ return err
4252 }
53+ return cmd .NewCommandMgr (cmd .WithTask (* task )).RunBashCf ("%s %s up -d" , global .CONF .DockerConfig .Command , loadFiles (filePath ))
54+ }
55+
56+ func pullComposeImages (filePath string , forcePull bool , task * task.Task ) error {
4357 content , err := os .ReadFile (filePath )
4458 if err != nil {
4559 return err
@@ -57,11 +71,28 @@ func UpWithTask(filePath string, task *task.Task, pullImages bool) error {
5771 if err != nil {
5872 return err
5973 }
60- errMsg := ""
6174 for _ , image := range images {
62- task .Log (i18n .GetWithName ("PullImageStart" , image ))
63- if err = dockerCLi .PullImageWithProcess (task , image ); err != nil {
64- errOur := err .Error ()
75+ if ! forcePull {
76+ if exist , _ := dockerCLi .ImageExists (image ); exist {
77+ if task != nil {
78+ task .Log (i18n .GetMsgByKey ("UseExistImage" ))
79+ }
80+ continue
81+ }
82+ }
83+
84+ if task != nil {
85+ task .Log (i18n .GetWithName ("PullImageStart" , image ))
86+ }
87+ pullErr := error (nil )
88+ if task != nil {
89+ pullErr = dockerCLi .PullImageWithProcess (task , image )
90+ } else {
91+ pullErr = docker .PullImage (image )
92+ }
93+ if pullErr != nil {
94+ errMsg := ""
95+ errOur := pullErr .Error ()
6596 if errOur != "" {
6697 if strings .Contains (errOur , "no such host" ) {
6798 errMsg = i18n .GetMsgByKey ("ErrNoSuchHost" ) + ":"
@@ -72,18 +103,21 @@ func UpWithTask(filePath string, task *task.Task, pullImages bool) error {
72103 }
73104 message := errMsg + errOur
74105 installErr := errors .New (message )
75- task .LogFailedWithErr (i18n .GetMsgByKey ("PullImage" ), installErr )
106+ if task != nil {
107+ task .LogFailedWithErr (i18n .GetMsgByKey ("PullImage" ), installErr )
108+ }
76109 if exist , _ := dockerCLi .ImageExists (image ); ! exist {
77110 return installErr
78- } else {
111+ }
112+ if task != nil {
79113 task .Log (i18n .GetMsgByKey ("UseExistImage" ))
80114 }
81- } else {
115+ } else if task != nil {
82116 task .Log (i18n .GetMsgByKey ("PullImageSuccess" ))
83117 }
84118 }
85119
86- return cmd . NewCommandMgr ( cmd . WithTask ( * task )). RunBashCf ( "%s %s up -d" , global . CONF . DockerConfig . Command , loadFiles ( filePath ))
120+ return nil
87121}
88122
89123func Down (filePath string ) (string , error ) {
0 commit comments