/
mod.go
87 lines (76 loc) · 2.8 KB
/
mod.go
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
// Copyright © 2019 Chris Monson <shiblon@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"log"
"entrogo.com/entroq"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
var flagMod = struct {
id string
queueTo string
val string
force bool
reset bool
}{}
func init() {
rootCmd.AddCommand(modCmd)
modCmd.Flags().StringVarP(&flagMod.id, "task", "t", "", "Task ID to modify. Note that this will modify *any* version of this task ID without regard for what else is happening. Use with care.")
modCmd.MarkFlagRequired("task")
modCmd.Flags().StringVarP(&flagMod.queueTo, "queue_to", "Q", "", "New queue for task, if a change is desired.")
modCmd.Flags().BoolVarP(&flagMod.reset, "reset", "r", false, "Reset Attempt and Err while changing the task, e.g., moving from an error queue back into service. Does not work on wrapped tasks (will just move the wrapping task, not the internals).")
modCmd.Flags().StringVarP(&flagMod.val, "val", "v", "", "Value to set in task.")
modCmd.Flags().BoolVarP(&flagMod.force, "force", "f", false, "Force by spoofing the claimant if already claimed.")
}
// modCmd represents the mod command
var modCmd = &cobra.Command{
Use: "mod",
Short: "Modify a task by queue and ID.",
Run: func(cmd *cobra.Command, args []string) {
id, err := uuid.Parse(flagMod.id)
if err != nil {
log.Fatalf("Error parsing task ID: %v", err)
}
tasks, err := eq.Tasks(context.Background(), "", entroq.WithTaskID(id))
if err != nil {
log.Fatalf("Error getting task ID %q", id)
}
if len(tasks) < 1 {
log.Fatalf("Could not find task ID %q", id)
}
if len(tasks) > 1 {
log.Fatalf("Too many tasks returned: %v", tasks)
}
task := tasks[0]
var chgArgs []entroq.ChangeArg
if flagMod.val != "" {
chgArgs = append(chgArgs, entroq.ValueTo([]byte(flagMod.val)))
}
if flagMod.queueTo != "" {
chgArgs = append(chgArgs, entroq.QueueTo(flagMod.queueTo))
}
if flagMod.reset {
chgArgs = append(chgArgs, entroq.AttemptToZero(), entroq.ErrToZero())
}
modArgs := []entroq.ModifyArg{task.AsChange(chgArgs...)}
if flagMod.force {
modArgs = append(modArgs, entroq.ModifyAs(task.Claimant))
}
if _, _, err := eq.Modify(context.Background(), modArgs...); err != nil {
log.Fatalf("Could not modify task %q: %v", id, err)
}
},
}