aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/util/task_output_mode.go
blob: eee42e0bc50d157aa27046c50e527b714a4d6dbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package util

import (
	"encoding/json"
	"fmt"
)

// TaskOutputMode defines the ways turbo can display task output during a run
type TaskOutputMode int

const (
	// FullTaskOutput will show all task output
	FullTaskOutput TaskOutputMode = iota
	// NoTaskOutput will hide all task output
	NoTaskOutput
	// HashTaskOutput will display turbo-computed task hashes
	HashTaskOutput
	// NewTaskOutput will show all new task output and turbo-computed task hashes for cached output
	NewTaskOutput
	// ErrorTaskOutput will show task output for failures only; no cache miss/hit messages are emitted
	ErrorTaskOutput
)

const (
	fullTaskOutputString  = "full"
	noTaskOutputString    = "none"
	hashTaskOutputString  = "hash-only"
	newTaskOutputString   = "new-only"
	errorTaskOutputString = "errors-only"
)

// TaskOutputModeStrings is an array containing the string representations for task output modes
var TaskOutputModeStrings = []string{
	fullTaskOutputString,
	noTaskOutputString,
	hashTaskOutputString,
	newTaskOutputString,
	errorTaskOutputString,
}

// FromTaskOutputModeString converts a task output mode's string representation into the enum value
func FromTaskOutputModeString(value string) (TaskOutputMode, error) {
	switch value {
	case fullTaskOutputString:
		return FullTaskOutput, nil
	case noTaskOutputString:
		return NoTaskOutput, nil
	case hashTaskOutputString:
		return HashTaskOutput, nil
	case newTaskOutputString:
		return NewTaskOutput, nil
	case errorTaskOutputString:
		return ErrorTaskOutput, nil
	}

	return FullTaskOutput, fmt.Errorf("invalid task output mode: %v", value)
}

// ToTaskOutputModeString converts a task output mode enum value into the string representation
func ToTaskOutputModeString(value TaskOutputMode) (string, error) {
	switch value {
	case FullTaskOutput:
		return fullTaskOutputString, nil
	case NoTaskOutput:
		return noTaskOutputString, nil
	case HashTaskOutput:
		return hashTaskOutputString, nil
	case NewTaskOutput:
		return newTaskOutputString, nil
	case ErrorTaskOutput:
		return errorTaskOutputString, nil
	}

	return "", fmt.Errorf("invalid task output mode: %v", value)
}

// UnmarshalJSON converts a task output mode string representation into an enum
func (c *TaskOutputMode) UnmarshalJSON(data []byte) error {
	var rawTaskOutputMode string
	if err := json.Unmarshal(data, &rawTaskOutputMode); err != nil {
		return err
	}

	taskOutputMode, err := FromTaskOutputModeString(rawTaskOutputMode)
	if err != nil {
		return err
	}

	*c = taskOutputMode
	return nil
}

// MarshalJSON converts a task output mode to its string representation
func (c TaskOutputMode) MarshalJSON() ([]byte, error) {
	outputModeString, err := ToTaskOutputModeString(c)
	if err != nil {
		return nil, err
	}
	return json.Marshal(outputModeString)
}