github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/builds.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	yaml "gopkg.in/yaml.v3"
     8  
     9  	"github.com/drycc/controller-sdk-go/builds"
    10  )
    11  
    12  // BuildsList lists an app's builds.
    13  func (d *DryccCmd) BuildsList(appID string, results int) error {
    14  	s, appID, err := load(d.ConfigFile, appID)
    15  
    16  	if err != nil {
    17  		return err
    18  	}
    19  
    20  	if results == defaultLimit {
    21  		results = s.Limit
    22  	}
    23  
    24  	builds, count, err := builds.List(s.Client, appID, results)
    25  	if d.checkAPICompatibility(s.Client, err) != nil {
    26  		return err
    27  	}
    28  	if count > 0 {
    29  		table := d.getDefaultFormatTable([]string{"UUID", "OWNER", "SHA", "CREATED"})
    30  		for _, build := range builds {
    31  			table.Append([]string{
    32  				build.UUID,
    33  				safeGetString(build.Owner),
    34  				safeGetString(build.Sha),
    35  				build.Created,
    36  			})
    37  		}
    38  		table.Render()
    39  	} else {
    40  		d.Println(fmt.Sprintf("No builds found in %s app.", appID))
    41  	}
    42  	return nil
    43  }
    44  
    45  // BuildsCreate creates a build for an app.
    46  func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile string) error {
    47  	s, appID, err := load(d.ConfigFile, appID)
    48  
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	procfileMap := make(map[string]string)
    54  
    55  	if procfile != "" {
    56  		if procfileMap, err = parseProcfile([]byte(procfile)); err != nil {
    57  			return err
    58  		}
    59  	} else if _, err := os.Stat("Procfile"); err == nil {
    60  		contents, err := os.ReadFile("Procfile")
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		if procfileMap, err = parseProcfile(contents); err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	d.Print("Creating build... ")
    71  	quit := progress(d.WOut)
    72  	_, err = builds.New(s.Client, appID, image, stack, procfileMap)
    73  	quit <- true
    74  	<-quit
    75  	if d.checkAPICompatibility(s.Client, err) != nil {
    76  		return err
    77  	}
    78  
    79  	d.Println("done")
    80  
    81  	return nil
    82  }
    83  
    84  func parseProcfile(procfile []byte) (map[string]string, error) {
    85  	procfileMap := make(map[string]string)
    86  	return procfileMap, yaml.Unmarshal(procfile, &procfileMap)
    87  }