| 
 | 1 | +// This file is part of arduino-cli.  | 
 | 2 | +//  | 
 | 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)  | 
 | 4 | +//  | 
 | 5 | +// This software is released under the GNU General Public License version 3,  | 
 | 6 | +// which covers the main part of arduino-cli.  | 
 | 7 | +// The terms of this license can be found at:  | 
 | 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html  | 
 | 9 | +//  | 
 | 10 | +// You can be released from the requirements of the above licenses by purchasing  | 
 | 11 | +// a commercial license. Buying such a license is mandatory if you want to  | 
 | 12 | +// modify or otherwise use the software for commercial activities involving the  | 
 | 13 | +// Arduino software without disclosing the source code of your own applications.  | 
 | 14 | +// To purchase a commercial license, send an email to license@arduino.cc.  | 
 | 15 | + | 
 | 16 | +package lib  | 
 | 17 | + | 
 | 18 | +import (  | 
 | 19 | +	"fmt"  | 
 | 20 | +	"os"  | 
 | 21 | +	"sort"  | 
 | 22 | +	"strings"  | 
 | 23 | + | 
 | 24 | +	"github.com/arduino/arduino-cli/cli/errorcodes"  | 
 | 25 | +	"github.com/arduino/arduino-cli/cli/feedback"  | 
 | 26 | +	"github.com/arduino/arduino-cli/cli/instance"  | 
 | 27 | +	"github.com/arduino/arduino-cli/commands/lib"  | 
 | 28 | +	rpc "github.com/arduino/arduino-cli/rpc/commands"  | 
 | 29 | +	"github.com/arduino/go-paths-helper"  | 
 | 30 | +	"github.com/fatih/color"  | 
 | 31 | +	"github.com/sirupsen/logrus"  | 
 | 32 | +	"github.com/spf13/cobra"  | 
 | 33 | +	"golang.org/x/net/context"  | 
 | 34 | +)  | 
 | 35 | + | 
 | 36 | +func initExamplesCommand() *cobra.Command {  | 
 | 37 | +	examplesCommand := &cobra.Command{  | 
 | 38 | +		Use:     "examples [LIBRARY_NAME]",  | 
 | 39 | +		Short:   "Shows the list of the examples for libraries.",  | 
 | 40 | +		Long:    "Shows the list of the examples for libraries. A name may be given as argument to search a specific library.",  | 
 | 41 | +		Example: "  " + os.Args[0] + " lib examples Wire",  | 
 | 42 | +		Args:    cobra.MaximumNArgs(1),  | 
 | 43 | +		Run:     runExamplesCommand,  | 
 | 44 | +	}  | 
 | 45 | +	examplesCommand.Flags().StringVarP(&examplesFlags.fqbn, "fqbn", "b", "", "Show libraries for the specified board FQBN.")  | 
 | 46 | +	return examplesCommand  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +var examplesFlags struct {  | 
 | 50 | +	fqbn string  | 
 | 51 | +}  | 
 | 52 | + | 
 | 53 | +func runExamplesCommand(cmd *cobra.Command, args []string) {  | 
 | 54 | +	instance := instance.CreateInstanceIgnorePlatformIndexErrors()  | 
 | 55 | +	logrus.Info("Show examples for library")  | 
 | 56 | + | 
 | 57 | +	name := ""  | 
 | 58 | +	if len(args) > 0 {  | 
 | 59 | +		name = args[0]  | 
 | 60 | +	}  | 
 | 61 | + | 
 | 62 | +	res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{  | 
 | 63 | +		Instance: instance,  | 
 | 64 | +		All:      true,  | 
 | 65 | +		Name:     name,  | 
 | 66 | +		Fqbn:     examplesFlags.fqbn,  | 
 | 67 | +	})  | 
 | 68 | +	if err != nil {  | 
 | 69 | +		feedback.Errorf("Error getting libraries info: %v", err)  | 
 | 70 | +		os.Exit(errorcodes.ErrGeneric)  | 
 | 71 | +	}  | 
 | 72 | + | 
 | 73 | +	found := []*libraryExamples{}  | 
 | 74 | +	for _, lib := range res.GetInstalledLibrary() {  | 
 | 75 | +		found = append(found, &libraryExamples{  | 
 | 76 | +			Library:  lib.Library,  | 
 | 77 | +			Examples: lib.Library.Examples,  | 
 | 78 | +		})  | 
 | 79 | +	}  | 
 | 80 | + | 
 | 81 | +	feedback.PrintResult(libraryExamplesResult{found})  | 
 | 82 | +	logrus.Info("Done")  | 
 | 83 | +}  | 
 | 84 | + | 
 | 85 | +// output from this command requires special formatting, let's create a dedicated  | 
 | 86 | +// feedback.Result implementation  | 
 | 87 | + | 
 | 88 | +type libraryExamples struct {  | 
 | 89 | +	Library  *rpc.Library `json:"library"`  | 
 | 90 | +	Examples []string     `json:"examples"`  | 
 | 91 | +}  | 
 | 92 | + | 
 | 93 | +type libraryExamplesResult struct {  | 
 | 94 | +	Examples []*libraryExamples  | 
 | 95 | +}  | 
 | 96 | + | 
 | 97 | +func (ir libraryExamplesResult) Data() interface{} {  | 
 | 98 | +	return ir.Examples  | 
 | 99 | +}  | 
 | 100 | + | 
 | 101 | +func (ir libraryExamplesResult) String() string {  | 
 | 102 | +	if ir.Examples == nil || len(ir.Examples) == 0 {  | 
 | 103 | +		return "No libraries found."  | 
 | 104 | +	}  | 
 | 105 | + | 
 | 106 | +	sort.Slice(ir.Examples, func(i, j int) bool {  | 
 | 107 | +		return strings.ToLower(ir.Examples[i].Library.Name) < strings.ToLower(ir.Examples[j].Library.Name)  | 
 | 108 | +	})  | 
 | 109 | + | 
 | 110 | +	res := []string{}  | 
 | 111 | +	for _, lib := range ir.Examples {  | 
 | 112 | +		name := lib.Library.Name  | 
 | 113 | +		if lib.Library.ContainerPlatform != "" {  | 
 | 114 | +			name += " (" + lib.Library.GetContainerPlatform() + ")"  | 
 | 115 | +		} else if lib.Library.Location != rpc.LibraryLocation_user {  | 
 | 116 | +			name += " (" + lib.Library.GetLocation().String() + ")"  | 
 | 117 | +		}  | 
 | 118 | +		r := fmt.Sprintf("Examples for library %s\n", color.GreenString("%s", name))  | 
 | 119 | +		sort.Slice(lib.Examples, func(i, j int) bool {  | 
 | 120 | +			return strings.ToLower(lib.Examples[i]) < strings.ToLower(lib.Examples[j])  | 
 | 121 | +		})  | 
 | 122 | +		for _, example := range lib.Examples {  | 
 | 123 | +			examplePath := paths.New(example)  | 
 | 124 | +			r += fmt.Sprintf("  - %s%s\n",  | 
 | 125 | +				color.New(color.Faint).Sprintf("%s%c", examplePath.Parent(), os.PathSeparator),  | 
 | 126 | +				examplePath.Base())  | 
 | 127 | +		}  | 
 | 128 | +		res = append(res, r)  | 
 | 129 | +	}  | 
 | 130 | + | 
 | 131 | +	return strings.Join(res, "\n")  | 
 | 132 | +}  | 
0 commit comments