github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/files/list.go (about)

     1  package files
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/Sirupsen/logrus"
     9  	"github.com/daticahealth/cli/commands/services"
    10  	"github.com/daticahealth/cli/models"
    11  	"github.com/olekukonko/tablewriter"
    12  )
    13  
    14  // CmdList lists all service files that are able to be downloaded
    15  // by a member of the environment. Typically service files of interest
    16  // will be on the service_proxy.
    17  func CmdList(svcName string, showTimestamps bool, ifiles IFiles, is services.IServices) error {
    18  	service, err := is.RetrieveByLabel(svcName)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	if service == nil {
    23  		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"datica services list\" command.", svcName)
    24  	}
    25  	files, err := ifiles.List(service.ID)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	if files == nil || len(*files) == 0 {
    30  		logrus.Println("No service files found")
    31  		return nil
    32  	}
    33  
    34  	const dateForm = "2006-01-02T15:04:05"
    35  	headerArray := []string{"NAME"}
    36  	if showTimestamps {
    37  		headerArray = append(headerArray, "CREATED_AT", "UPDATED_AT")
    38  	}
    39  	data := [][]string{headerArray}
    40  	for _, sf := range *files {
    41  		line := []string{sf.Name}
    42  		if showTimestamps {
    43  			ct, _ := time.Parse(dateForm, sf.CreatedAt)
    44  			ut, _ := time.Parse(dateForm, sf.UpdatedAt)
    45  			line = append(line, ct.Local().Format(time.ANSIC), ut.Local().Format(time.ANSIC))
    46  		}
    47  		data = append(data, line)
    48  	}
    49  
    50  	table := tablewriter.NewWriter(logrus.StandardLogger().Out)
    51  	table.SetBorder(false)
    52  	table.SetRowLine(false)
    53  	table.SetCenterSeparator("")
    54  	table.SetColumnSeparator("")
    55  	table.SetRowSeparator("")
    56  	table.AppendBulk(data)
    57  	table.Render()
    58  
    59  	logrus.Printf("\nTo view the contents of a service file, use the \"datica files download %s FILE_NAME\" command", svcName)
    60  	return nil
    61  }
    62  
    63  func fileModeToRWXString(perms uint64) string {
    64  	permissionString := ""
    65  	binaryString := strconv.FormatUint(perms, 2)
    66  	for i := 0; i < 3; i++ {
    67  		for j := 0; j < 3; j++ {
    68  			if string(binaryString[len(binaryString)-1-i*3-j]) == "1" {
    69  				switch j {
    70  				case 0:
    71  					permissionString = "x" + permissionString
    72  				case 1:
    73  					permissionString = "w" + permissionString
    74  				case 2:
    75  					permissionString = "r" + permissionString
    76  				}
    77  			} else {
    78  				permissionString = "-" + permissionString
    79  			}
    80  		}
    81  	}
    82  	permissionString = "-" + permissionString // we don't store folders
    83  	return permissionString
    84  }
    85  
    86  func (f *SFiles) List(svcID string) (*[]models.ServiceFile, error) {
    87  	headers := f.Settings.HTTPManager.GetHeaders(f.Settings.SessionToken, f.Settings.Version, f.Settings.Pod, f.Settings.UsersID)
    88  	resp, statusCode, err := f.Settings.HTTPManager.Get(nil, fmt.Sprintf("%s%s/environments/%s/services/%s/files", f.Settings.PaasHost, f.Settings.PaasHostVersion, f.Settings.EnvironmentID, svcID), headers)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	var svcFiles []models.ServiceFile
    93  	err = f.Settings.HTTPManager.ConvertResp(resp, statusCode, &svcFiles)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	return &svcFiles, nil
    98  }