-
Notifications
You must be signed in to change notification settings - Fork 0
release: 0.7.0 #17
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
Merged
Merged
release: 0.7.0 #17
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| ".": "0.6.1" | ||
| ".": "0.7.0" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "net/http/httputil" | ||
| "os" | ||
| "reflect" | ||
| "strings" | ||
|
|
||
| "github.com/onkernel/hypeman-go/option" | ||
|
|
||
| "github.com/tidwall/sjson" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
|
|
||
| type fileReader struct { | ||
| Value io.Reader | ||
| Base64Encoded bool | ||
| } | ||
|
|
||
| func (f *fileReader) Set(filename string) error { | ||
| reader, err := os.Open(filename) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read file %q: %w", filename, err) | ||
| } | ||
| f.Value = reader | ||
| return nil | ||
| } | ||
|
|
||
| func (f *fileReader) String() string { | ||
| if f.Value == nil { | ||
| return "" | ||
| } | ||
| buf := new(bytes.Buffer) | ||
| buf.ReadFrom(f.Value) | ||
| if f.Base64Encoded { | ||
| return base64.StdEncoding.EncodeToString(buf.Bytes()) | ||
| } | ||
| return buf.String() | ||
| } | ||
|
|
||
| func (f *fileReader) Get() any { | ||
| return f.String() | ||
| } | ||
|
|
||
| func unmarshalWithReaders(data []byte, v any) error { | ||
| var fields map[string]json.RawMessage | ||
| if err := json.Unmarshal(data, &fields); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rv := reflect.ValueOf(v).Elem() | ||
| rt := rv.Type() | ||
|
|
||
| for i := 0; i < rv.NumField(); i++ { | ||
| fv := rv.Field(i) | ||
| ft := rt.Field(i) | ||
|
|
||
| jsonKey := ft.Tag.Get("json") | ||
| if jsonKey == "" { | ||
| jsonKey = ft.Name | ||
| } else if idx := strings.Index(jsonKey, ","); idx != -1 { | ||
| jsonKey = jsonKey[:idx] | ||
| } | ||
|
|
||
| rawVal, ok := fields[jsonKey] | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| if ft.Type == reflect.TypeOf((*io.Reader)(nil)).Elem() { | ||
| var s string | ||
| if err := json.Unmarshal(rawVal, &s); err != nil { | ||
| return fmt.Errorf("field %s: %w", ft.Name, err) | ||
| } | ||
| fv.Set(reflect.ValueOf(strings.NewReader(s))) | ||
| } else { | ||
| ptr := fv.Addr().Interface() | ||
| if err := json.Unmarshal(rawVal, ptr); err != nil { | ||
| return fmt.Errorf("field %s: %w", ft.Name, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func unmarshalStdinWithFlags(cmd *cli.Command, flags map[string]string, target any) error { | ||
| var data []byte | ||
| if isInputPiped() { | ||
| var err error | ||
| if data, err = io.ReadAll(os.Stdin); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Merge CLI flags into the body | ||
| for flag, path := range flags { | ||
| if cmd.IsSet(flag) { | ||
| var err error | ||
| data, err = sjson.SetBytes(data, path, cmd.Value(flag)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if data != nil { | ||
| if err := unmarshalWithReaders(data, target); err != nil { | ||
| return fmt.Errorf("failed to unmarshal JSON: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func debugMiddleware(debug bool) option.Middleware { | ||
| return func(r *http.Request, mn option.MiddlewareNext) (*http.Response, error) { | ||
| if debug { | ||
| logger := log.Default() | ||
|
|
||
| if reqBytes, err := httputil.DumpRequest(r, true); err == nil { | ||
| logger.Printf("Request Content:\n%s\n", reqBytes) | ||
| } | ||
|
|
||
| resp, err := mn(r) | ||
| if err != nil { | ||
| return resp, err | ||
| } | ||
|
|
||
| if respBytes, err := httputil.DumpResponse(resp, true); err == nil { | ||
| logger.Printf("Response Content:\n%s\n", respBytes) | ||
| } | ||
|
|
||
| return resp, err | ||
| } | ||
|
|
||
| return mn(r) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Reader consumed on first access returns empty thereafter
The
fileReader.String()method callsbuf.ReadFrom(f.Value)which fully consumes the underlyingio.Reader. SinceGet()delegates toString(), any subsequent call to either method returns an empty string. CLI frameworks often access flag values multiple times (for help text, validation, actual usage), causing silent data loss after the first read.