github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/runcmd.go (about)

     1  // Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved.
     2  //
     3  // Licensed under the MIT License
     4  //
     5  //
     6  // Permission is hereby granted, free of charge, to any person obtaining a copy
     7  // of this software and associated documentation files (the "Software"), to deal
     8  // in the Software without restriction, including without limitation the rights
     9  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  // copies of the Software, and to permit persons to whom the Software is
    11  // furnished to do so, subject to the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be included in all
    14  // copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  // SOFTWARE.
    23  
    24  // collection of comands they can be used for any comand interpreter like cobra and ishell.
    25  package taskrun
    26  
    27  import (
    28  	"fmt"
    29  	"strings"
    30  
    31  	"github.com/swaros/contxt/module/configure"
    32  	"github.com/swaros/contxt/module/dirhandle"
    33  	"github.com/swaros/manout"
    34  )
    35  
    36  func PrintCnPaths() {
    37  	fmt.Println(manout.MessageCln("\t", "paths stored in ", manout.ForeCyan, configure.GetGlobalConfig().UsedV2Config.CurrentSet))
    38  	dir, err := dirhandle.Current()
    39  	if err == nil {
    40  		ShowPaths(dir)
    41  	}
    42  }
    43  
    44  // ShowPaths : display all stored paths in the workspace
    45  func ShowPaths(current string) {
    46  	configure.GetGlobalConfig().PathWorkerNoCd(func(index string, path string) {
    47  		if path == current {
    48  			fmt.Println(manout.MessageCln("\t[", manout.ForeLightYellow, index, manout.CleanTag, "]\t", manout.BoldTag, path))
    49  		} else {
    50  			fmt.Println(manout.MessageCln("\t ", manout.ForeLightBlue, index, manout.CleanTag, " \t", path))
    51  		}
    52  
    53  	})
    54  }
    55  
    56  func GetAllTargets() ([]string, bool) {
    57  	plainTargets, found := targetsAsMap()
    58  	template, _, exists, terr := GetTemplate()
    59  	if terr != nil {
    60  		return plainTargets, found
    61  	}
    62  	if exists {
    63  		shareds := detectSharedTargetsAsMap(template)
    64  		plainTargets = append(plainTargets, shareds...)
    65  	}
    66  	return plainTargets, exists && found
    67  }
    68  
    69  func detectSharedTargetsAsMap(current configure.RunConfig) []string {
    70  	var targets []string
    71  	SharedFolderExecuter(current, func(_, _ string) {
    72  		sharedTargets, have := targetsAsMap()
    73  		if have {
    74  			targets = append(targets, sharedTargets...)
    75  		}
    76  	})
    77  
    78  	return targets
    79  }
    80  
    81  func ExistInStrMap(testStr string, check []string) bool {
    82  	for _, str := range check {
    83  		if strings.TrimSpace(str) == strings.TrimSpace(testStr) {
    84  			return true
    85  		}
    86  	}
    87  	return false
    88  }
    89  
    90  func targetsAsMap() ([]string, bool) {
    91  	var targets []string
    92  	template, _, exists, terr := GetTemplate()
    93  	if terr != nil {
    94  		targets = append(targets, terr.Error())
    95  		return targets, false
    96  	}
    97  	if exists {
    98  		return templateTargetsAsMap(template)
    99  	}
   100  	return targets, false
   101  }
   102  
   103  func templateTargetsAsMap(template configure.RunConfig) ([]string, bool) {
   104  	var targets []string
   105  	found := false
   106  
   107  	if len(template.Task) > 0 {
   108  		for _, tasks := range template.Task {
   109  			if !ExistInStrMap(tasks.ID, targets) && (!tasks.Options.Invisible || showInvTarget) {
   110  				found = true
   111  				targets = append(targets, strings.TrimSpace(tasks.ID))
   112  			}
   113  		}
   114  	}
   115  
   116  	return targets, found
   117  }
   118  
   119  func printTargets() {
   120  
   121  	template, path, exists, terr := GetTemplate()
   122  	if terr != nil {
   123  		return
   124  	}
   125  	if exists {
   126  		fmt.Println(manout.MessageCln(manout.ForeDarkGrey, "used taskfile:\t", manout.CleanTag, path))
   127  		fmt.Println(manout.MessageCln(manout.ForeDarkGrey, "tasks count:  \t", manout.CleanTag, len(template.Task)))
   128  		if len(template.Task) > 0 {
   129  			fmt.Println(manout.MessageCln(manout.BoldTag, "existing targets:"))
   130  			taskList, _ := templateTargetsAsMap(template)
   131  			for _, tasks := range taskList {
   132  				fmt.Println("\t", tasks)
   133  			}
   134  		} else {
   135  			fmt.Println(manout.MessageCln("that is what we gor so far:"))
   136  			fmt.Println()
   137  		}
   138  
   139  		sharedTargets := detectSharedTargetsAsMap(template)
   140  		if len(sharedTargets) > 0 {
   141  
   142  			for _, stasks := range sharedTargets {
   143  				fmt.Println("\t", stasks, manout.MessageCln(manout.ForeDarkGrey, " shared", manout.CleanTag))
   144  			}
   145  
   146  		}
   147  	} else {
   148  		fmt.Println(manout.MessageCln(manout.ForeCyan, "no task-file exists. you can create one by ", manout.CleanTag, " contxt create"))
   149  	}
   150  }
   151  
   152  func printPaths() {
   153  	dir, err := dirhandle.Current()
   154  	if err == nil {
   155  		fmt.Println(manout.MessageCln(manout.ForeWhite, " current directory: ", manout.BoldTag, dir))
   156  		fmt.Println(manout.MessageCln(manout.ForeWhite, " current workspace: ", manout.BoldTag, configure.GetGlobalConfig().UsedV2Config.CurrentSet))
   157  		notWorkspace := true
   158  		pathColor := manout.ForeLightBlue
   159  		if !configure.GetGlobalConfig().PathMeightPartOfWs(dir) {
   160  			pathColor = manout.ForeLightMagenta
   161  		} else {
   162  			notWorkspace = false
   163  		}
   164  		fmt.Println(" contains paths:")
   165  		configure.GetGlobalConfig().PathWorker(func(index string, path string) {
   166  			template, _, exists, _ := GetTemplate()
   167  			add := ""
   168  			if strings.Contains(dir, path) {
   169  				add = manout.ResetDim + manout.ForeCyan
   170  			}
   171  			if dir == path {
   172  				add = manout.ResetDim + manout.ForeGreen
   173  			}
   174  			if exists {
   175  				outTasks := ""
   176  				targets, _ := templateTargetsAsMap(template)
   177  				for _, tasks := range targets {
   178  					outTasks = outTasks + " " + tasks
   179  				}
   180  
   181  				fmt.Println(manout.MessageCln("       path: ", manout.Dim, " no ", manout.ForeYellow, index, " ", pathColor, add, path, manout.CleanTag, " targets", "[", manout.ForeYellow, outTasks, manout.CleanTag, "]"))
   182  			} else {
   183  				fmt.Println(manout.MessageCln("       path: ", manout.Dim, " no ", manout.ForeYellow, index, " ", pathColor, add, path))
   184  			}
   185  		}, func(origin string) {})
   186  		if notWorkspace {
   187  			fmt.Println()
   188  			fmt.Println(manout.MessageCln(manout.BackYellow, manout.ForeBlue, " WARNING ! ", manout.CleanTag, "\tyou are currently in none of the assigned locations."))
   189  			fmt.Println("\t\tso maybe you are using the wrong workspace")
   190  		}
   191  		if !showHints {
   192  			fmt.Println()
   193  			fmt.Println(manout.MessageCln(" targets can be executes by ", manout.BoldTag, "contxt run <targetname>", manout.CleanTag, "(for the current directory)"))
   194  			fmt.Println(manout.MessageCln(" a target can also be executed in all stored paths by ", manout.BoldTag, "contxt run -a <targetname>", manout.CleanTag, " independend from current path"))
   195  		}
   196  
   197  		fmt.Println()
   198  		if !showHints {
   199  			fmt.Println(manout.MessageCln(" all workspaces:", " ... change by ", manout.BoldTag, "contxt <workspace>", ""))
   200  		} else {
   201  			fmt.Println(manout.MessageCln(" all workspaces:"))
   202  		}
   203  		configure.GetGlobalConfig().ExecOnWorkSpaces(func(index string, cfg configure.ConfigurationV2) {
   204  			if cfg.Name == configure.GetGlobalConfig().UsedV2Config.CurrentSet {
   205  				fmt.Println(manout.MessageCln("\t[ ", manout.BoldTag, cfg.Name, manout.CleanTag, " ]"))
   206  			} else {
   207  				fmt.Println(manout.MessageCln("\t  ", cfg.Name, "   "))
   208  			}
   209  		})
   210  	}
   211  }