Skip to content

Commit

Permalink
Allowing capture the output of certain command (#1239)
Browse files Browse the repository at this point in the history
Fixes issue about argument parsing and subcommand name as well.

Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
  • Loading branch information
sighingnow committed Mar 7, 2023
1 parent 6427c60 commit 631f917
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
4 changes: 2 additions & 2 deletions k8s/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ vineyardctl: generate fmt

# Copy the artifacts into the python directory for bdist_wheel.
bdist_wheel:
@cp vineyardctl ../python/vineyard/vineyardctl
@strip vineyardctl
@cp vineyardctl ../python/vineyard/bdist/vineyardctl
@strip ../python/vineyard/bdist/vineyardctl

# Run vineyardctl binary
run: generate fmt
Expand Down
3 changes: 2 additions & 1 deletion k8s/cmd/commands/schedule/schedule_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ var scheduleWorkloadCmd = &cobra.Command{
log.Fatal(err, "failed to schedule workload")
}

log.Info(workload)
log.Output(workload)
},
}

Expand Down Expand Up @@ -161,6 +161,7 @@ func ValidateWorkloadKind(kind string) bool {
}
return !isWorkload
}

func validateWorkload(workload string) (bool, error) {
isWorkload := true
decoder := util.Deserializer()
Expand Down
16 changes: 16 additions & 0 deletions k8s/pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func (l Logger) Fatalf(err error, format string, v ...any) {
l.Fatal(err, fmt.Sprintf(format, v...))
}

func (l Logger) Output(msg string) {
fmt.Println(msg)
}

func (l Logger) Outputf(format string, v ...any) {
fmt.Printf(format, v...)
}

func Info(msg string, keysAndValues ...any) {
Log.Info(msg, keysAndValues...)
}
Expand All @@ -117,3 +125,11 @@ func Errorf(err error, format string, v ...any) {
func Fatalf(err error, format string, v ...any) {
Log.Fatalf(err, format, v...)
}

func Output(msg string) {
Log.Output(msg)
}

func Outputf(msg string, keysAndValues ...any) {
Log.Outputf(msg, keysAndValues...)
}
37 changes: 26 additions & 11 deletions python/vineyard/deploy/_cobra.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def infer_signature(name, usage, exclude_args):
if default_value_rep:
argument_help += " Defaults to " + default_value_rep + "."
argument_docs.append(textwrap.indent(argument_help, " "))
kwargs.append('capture=False') # allow capturing the stdout
return name + "(" + ", ".join(kwargs) + ")", arguments, defaults, argument_docs


Expand Down Expand Up @@ -105,7 +106,7 @@ def make_command(executable, usage, exclude_args, scope=None):
doc = infer_docstring(name, usage, argument_docs)

@with_signature(signature, func_name=name, doc=doc)
def cmd(*args, **kwargs):
def cmd(*args, capture=False, **kwargs):
if usage["Runnable"]:
command_and_args = [executable]
command_and_args.extend(scope)
Expand All @@ -117,22 +118,36 @@ def cmd(*args, **kwargs):
if key in defaults and value == defaults[key]:
continue
command_and_args.append("--" + arguments.get(key, key))
command_and_args.append(json.dumps(value))
if not isinstance(value, str):
value = json.dumps(value)
command_and_args.append(value)
logger.debug('Executing: %s', ' '.join(command_and_args))
return subprocess.check_call(
command_and_args,
universal_newlines=True,
encoding="utf-8",
errors="ignore",
shell=False,
stderr=subprocess.STDOUT,
)
parameters = {
'args': command_and_args,
'universal_newlines': True,
'encoding': "utf-8",
'errors': "ignore",
'shell': False,
}
if capture:
output = subprocess.check_output(
**parameters,
)
if output:
output = output.strip()
return output
else:
return subprocess.check_call(
**parameters,
bufsize=1,
stderr=subprocess.STDOUT,
)

# generate subcommands
if scope is None:
scope = []
for child in usage["Children"]:
child_scope = scope + [infer_name(child['Name'])]
child_scope = scope + [child['Name']]
child_name, child_command = make_command(
executable, child, exclude_args, child_scope
)
Expand Down

0 comments on commit 631f917

Please sign in to comment.