github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/shared/state.go (about) 1 package shared 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/ungtb10d/cli/v2/api" 8 "github.com/ungtb10d/cli/v2/pkg/iostreams" 9 ) 10 11 type metadataStateType int 12 13 const ( 14 IssueMetadata metadataStateType = iota 15 PRMetadata 16 ) 17 18 type IssueMetadataState struct { 19 Type metadataStateType 20 21 Draft bool 22 23 Body string 24 Title string 25 26 Metadata []string 27 Reviewers []string 28 Assignees []string 29 Labels []string 30 Projects []string 31 Milestones []string 32 33 MetadataResult *api.RepoMetadataResult 34 35 dirty bool // whether user i/o has modified this 36 } 37 38 func (tb *IssueMetadataState) MarkDirty() { 39 tb.dirty = true 40 } 41 42 func (tb *IssueMetadataState) IsDirty() bool { 43 return tb.dirty || tb.HasMetadata() 44 } 45 46 func (tb *IssueMetadataState) HasMetadata() bool { 47 return len(tb.Reviewers) > 0 || 48 len(tb.Assignees) > 0 || 49 len(tb.Labels) > 0 || 50 len(tb.Projects) > 0 || 51 len(tb.Milestones) > 0 52 } 53 54 func FillFromJSON(io *iostreams.IOStreams, recoverFile string, state *IssueMetadataState) error { 55 var data []byte 56 var err error 57 data, err = io.ReadUserFile(recoverFile) 58 if err != nil { 59 return fmt.Errorf("failed to read file %s: %w", recoverFile, err) 60 } 61 62 err = json.Unmarshal(data, state) 63 if err != nil { 64 return fmt.Errorf("JSON parsing failure: %w", err) 65 } 66 67 return nil 68 }