mirror of
https://github.com/go-task/task.git
synced 2025-05-13 22:16:31 +02:00
Implemented os specific task file support
This commit is contained in:
parent
f686c7598f
commit
6636cd38c0
@ -4,8 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrNoTaskFile is returned when the program can not find a proper TaskFile
|
type taskFileNotFound struct {
|
||||||
var ErrNoTaskFile = fmt.Errorf(`task: No task file found (is it named "%s"?)`, TaskFilePath)
|
taskFile string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err taskFileNotFound) Error() string {
|
||||||
|
return fmt.Sprintf(`task: No task file found (is it named "%s"?)`, err.taskFile)
|
||||||
|
}
|
||||||
|
|
||||||
type taskNotFoundError struct {
|
type taskNotFoundError struct {
|
||||||
taskName string
|
taskName string
|
||||||
|
58
read_taskfile.go
Normal file
58
read_taskfile.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package task
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
|
"github.com/imdario/mergo"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readTaskfile() (map[string]*Task, error) {
|
||||||
|
initialTasks, err := readTaskfileData(TaskFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mergeTasks, err := readTaskfileData(fmt.Sprintf("%s_%s", TaskFilePath, runtime.GOOS))
|
||||||
|
if err != nil {
|
||||||
|
switch err.(type) {
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
case taskFileNotFound:
|
||||||
|
return initialTasks, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := mergo.MapWithOverwrite(&initialTasks, mergeTasks); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return initialTasks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readTaskfileData(path string) (tasks map[string]*Task, err error) {
|
||||||
|
if b, err := ioutil.ReadFile(path + ".yml"); err == nil {
|
||||||
|
return tasks, yaml.Unmarshal(b, &tasks)
|
||||||
|
}
|
||||||
|
if b, err := ioutil.ReadFile(path + ".json"); err == nil {
|
||||||
|
return tasks, json.Unmarshal(b, &tasks)
|
||||||
|
}
|
||||||
|
if b, err := ioutil.ReadFile(path + ".toml"); err == nil {
|
||||||
|
return tasks, toml.Unmarshal(b, &tasks)
|
||||||
|
}
|
||||||
|
return nil, taskFileNotFound{path}
|
||||||
|
}
|
||||||
|
|
||||||
|
func exists(path string) (bool, error) {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return true, err
|
||||||
|
}
|
17
task.go
17
task.go
@ -1,16 +1,12 @@
|
|||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"gopkg.in/yaml.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -168,16 +164,3 @@ func (t *Task) runCommand(i int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readTaskfile() (tasks map[string]*Task, err error) {
|
|
||||||
if b, err := ioutil.ReadFile(TaskFilePath + ".yml"); err == nil {
|
|
||||||
return tasks, yaml.Unmarshal(b, &tasks)
|
|
||||||
}
|
|
||||||
if b, err := ioutil.ReadFile(TaskFilePath + ".json"); err == nil {
|
|
||||||
return tasks, json.Unmarshal(b, &tasks)
|
|
||||||
}
|
|
||||||
if b, err := ioutil.ReadFile(TaskFilePath + ".toml"); err == nil {
|
|
||||||
return tasks, toml.Unmarshal(b, &tasks)
|
|
||||||
}
|
|
||||||
return nil, ErrNoTaskFile
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user