github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/controller/unsplash.go (about)

     1  /*
     2  Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7      http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package controller
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/awslabs/clencli/cobra/aid"
    23  	"github.com/awslabs/clencli/cobra/dao"
    24  	"github.com/awslabs/clencli/cobra/model"
    25  	"github.com/awslabs/clencli/helper"
    26  	"github.com/sirupsen/logrus"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var unsplashPhotoSizes = []string{"all", "thumb", "small", "regular", "full", "raw"}
    31  
    32  // UnsplashCmd command to download photos from Unsplash.com
    33  func UnsplashCmd() *cobra.Command {
    34  	man, err := helper.GetManual("unsplash")
    35  	if err != nil {
    36  		fmt.Println(err)
    37  		os.Exit(1)
    38  	}
    39  
    40  	cmd := &cobra.Command{
    41  		Use:     man.Use,
    42  		Short:   man.Short,
    43  		Long:    man.Long,
    44  		Example: man.Example,
    45  		PreRunE: unsplashPreRun,
    46  		RunE:    unsplashRun,
    47  	}
    48  
    49  	cmd.Flags().String("id", "", "The photo’s ID. Leave it empty if you want to download a random photo instead")
    50  	cmd.Flags().String("collections", "", "Public collection ID(‘s) to filter selection. If multiple, comma-separated")
    51  	cmd.Flags().Bool("featured", false, "Limit selection to featured photos. Valid values: false, true.")
    52  	cmd.Flags().String("filter", "low", "Limit results by content safety. Default: low. Valid values are low and high.")
    53  	cmd.Flags().String("orientation", "landscape", "Filter by photo orientation. Valid values: landscape, portrait, squarish.")
    54  	cmd.Flags().String("query", "mountains", "Limit selection to photos matching a search term.")
    55  	cmd.Flags().String("size", "all", "Photos size. Valid values: all, thumb, small, regular, full, raw. Default: all")
    56  	cmd.Flags().String("username", "", "Limit selection to a single user.")
    57  
    58  	return cmd
    59  }
    60  
    61  func unsplashPreRun(cmd *cobra.Command, args []string) error {
    62  	logrus.Traceln("start: command unsplash pre-run")
    63  
    64  	params := aid.GetModelFromFlags(cmd)
    65  	if !helper.ContainsString(unsplashPhotoSizes, params.Size) {
    66  		return fmt.Errorf("unknown photo size provided: %s", params.Size)
    67  	}
    68  
    69  	logrus.Traceln("end: command unsplash pre-run")
    70  
    71  	return nil
    72  }
    73  
    74  func unsplashRun(cmd *cobra.Command, args []string) error {
    75  	logrus.Traceln("start: command unsplash run")
    76  
    77  	cred, err := dao.GetCredentialByProvider(profile, "unsplash")
    78  	if err != nil {
    79  		logrus.Errorf("Unexpected error: %v", err)
    80  		return err
    81  	}
    82  
    83  	if (model.Credential{}) == cred {
    84  		return fmt.Errorf("no unsplash credential found or no profile enabled")
    85  	}
    86  
    87  	id, err := cmd.Flags().GetString("id")
    88  	if err != nil {
    89  		return fmt.Errorf("unable to get flag id\n%v", err)
    90  	}
    91  
    92  	if id != "" {
    93  		// get photo
    94  		response, err := aid.GetPhoto(id, cred)
    95  		if err != nil || response.ID == "" {
    96  			return fmt.Errorf("unablet to get photo\n%v", err)
    97  		}
    98  
    99  		size, err := cmd.Flags().GetString("size")
   100  		if err != nil {
   101  			return fmt.Errorf("unable to get flag id\n%v", err)
   102  		}
   103  
   104  		if response.ID != "" {
   105  			aid.SaveGetPhotoResult(response)
   106  			aid.DownloadPhotoByID(response, size)
   107  		}
   108  
   109  	} else {
   110  		// random photo
   111  		params := aid.GetModelFromFlags(cmd)
   112  		err = aid.DownloadPhoto(params, cred, unsplashPhotoSizes)
   113  		if err != nil {
   114  			logrus.Errorf("unable to download photo\n%v", err)
   115  			return err
   116  		}
   117  
   118  	}
   119  
   120  	logrus.Traceln("end: command unsplash run")
   121  	return err
   122  }