github.com/kcmerrill/alfred@v0.0.0-20180727171036-06445dcb5e3d/pkg/alfred/fetchTasks.go (about)

     1  package alfred
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/kcmerrill/common.go/config"
     9  	"github.com/kcmerrill/common.go/file"
    10  	yaml "gopkg.in/yaml.v2"
    11  )
    12  
    13  // AddTasks to the current task list
    14  func AddTasks(contents []byte, context *Context, tasks map[string]Task) map[string]Task {
    15  	var fetched map[string]Task
    16  	err := yaml.Unmarshal(contents, &fetched)
    17  	if err != nil {
    18  		outFail("yaml", "Unable to unmarshal", context)
    19  		outFail("yaml", "{{ .Text.Failure }}"+err.Error(), context)
    20  		os.Exit(42)
    21  	}
    22  
    23  	context.lock.Lock()
    24  	for fetchedTaskName, fetchedTask := range fetched {
    25  		tasks[fetchedTaskName] = fetchedTask
    26  	}
    27  	context.lock.Unlock()
    28  
    29  	return tasks
    30  }
    31  
    32  // FetchTask will fetch the tasks
    33  func FetchTask(task string, context *Context, tasks map[string]Task) (string, Task, map[string]Task) {
    34  	if t, exists := tasks[task]; exists {
    35  		return "./", t, tasks
    36  	}
    37  
    38  	if strings.HasPrefix(task, "!") {
    39  		context.TaskName = "exec.command"
    40  		return "./", Task{Summary: "Executing Command", Command: task[1:len(task)], ExitCode: 42}, tasks
    41  	}
    42  
    43  	var location string
    44  	var contents []byte
    45  
    46  	location, task = TaskParser(task, "alfred:list")
    47  
    48  	// hmmm, the task does not exist. Lets try to load whatever possible
    49  	if strings.HasPrefix(location, "http") {
    50  		f, err := file.Get(location)
    51  		if err != nil {
    52  			// cannot use output, no task yet ...
    53  			fmt.Println(translate("{{ .Text.Failure }}{{ .Text.FailureIcon }} "+location+"{{ .Text.Reset }}", emptyContext()))
    54  			os.Exit(42)
    55  		}
    56  		contents = f
    57  	} else {
    58  		catalog := ""
    59  		// must be local? catalog?
    60  		if isCatalog(location) {
    61  			catalog = strings.TrimLeft(location, "@") + "/"
    62  		}
    63  
    64  		foundDir, local, err := config.FindAndCombine(context.rootDir, catalog+context.FileName, "yml")
    65  		if err != nil {
    66  			// cannot use output, no task yet ...
    67  			fmt.Println(translate("{{ .Text.Failure }}{{ .Text.FailureIcon }} Missing task file.{{ .Text.Reset }}", emptyContext()))
    68  			os.Exit(42)
    69  		}
    70  
    71  		// ok, we found a good catalog, lets update it
    72  		if isCatalog(location) && context.hasBeenInited {
    73  			updateCatalog(foundDir, context)
    74  			foundDir, local, err = config.FindAndCombine(foundDir, catalog+context.FileName, "yml")
    75  		}
    76  
    77  		contents = local
    78  		location = foundDir
    79  		context.rootDir = location
    80  	}
    81  
    82  	tasks = AddTasks(contents, context, tasks)
    83  
    84  	if task == "__init" || task == "__exit" {
    85  		return location, Task{Skip: true}, tasks
    86  	}
    87  
    88  	if task == "alfred:list" {
    89  		list(context, tasks)
    90  		os.Exit(0)
    91  	}
    92  
    93  	if t, exists := tasks[task]; exists {
    94  		return location, t, tasks
    95  	}
    96  
    97  	outFail("invalid task", "{{ .Text.Failure }}'"+task+"'", context)
    98  	os.Exit(42)
    99  	return "./", Task{}, tasks
   100  }