Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Specific Flags #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion out.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ func Dump(out io.Writer) error {
})
return parser.Serialize(vals, out)
}
// DumpMyFlag will write all flags contained in the flags array to the given io.Writer in the flagfile
// serialization format for later parsing.
func DumpMyFlags(myflags []string,out io.Writer) error{
mtx.Lock()
defer mtx.Unlock()
vals := make(map[string]string)

flag.VisitAll(func (f *flag.Flag){
for _,flg := range myflags {
if f.Name == flg {
vals[f.Name] = f.Value.String()
}
}

})
return parser.Serialize(vals,out)
}

// DumpToPath simply calls DumpMyFlags on a new filehandle (O_CREATE|O_TRUNC) for the
// given path
func DumpMyFlagsToPath(myflags []string,path string) error{
fh, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0600)
if err != nil {
return err
}
defer fh.Close()

return DumpMyFlags(myflags,fh)
}


// DumpToPath simply calls Dump on a new filehandle (O_CREATE|O_TRUNC) for the
// given path
Expand All @@ -61,4 +92,4 @@ func DumpToPath(path string) error {
}
defer fh.Close()
return Dump(fh)
}
}