Skip to content
Myr

Add OnFinalize method (#1788)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This method is the OnInitialize counterpart. Like OnInitialize which allows loading the configuration before each command is executed, OnFinalize allows saving the configuration after each command has been executed.

authored by Yann Soubeyrand · committed by GitHub · parent 07034fe · Browse files · 2 files+15 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+15 added−0 removed

  • other:+15 added−0 removed

2 files+15 added−0 removed

modified:cobra.go+7 added
38 unmodified lines
39 "eq": Eq,40}4142var initializers []func()43var finalizers []func()4445const (46 defaultPrefixMatching = false47 defaultCommandSorting = true
46 unmodified lines
94func OnInitialize(y ...func()) {95 initializers = append(initializers, y...)96}9798// OnFinalize sets the passed functions to be run when each command's99// Execute method is terminated.100func OnFinalize(y ...func()) {101 finalizers = append(finalizers, y...)102}103104// FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.105106// Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,107// Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
132 unmodified lines
modified:command.go+8 added
832 unmodified lines
833 }834835 c.preRun()836837 defer c.postRun()838839 argWoFlags := c.Flags().Args()840 if c.DisableFlagParsing {841 argWoFlags = a842 }
62 unmodified lines
905 x()906 }907}908909func (c *Command) postRun() {910 for _, x := range finalizers {911 x()912 }913}914915// ExecuteContext is the same as Execute(), but sets the ctx on the command.916// Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs917// functions.918func (c *Command) ExecuteContext(ctx context.Context) error {
801 unmodified lines