|
| 1 | +/* |
| 2 | + * This file is part of arduino-cli. |
| 3 | + * |
| 4 | + * arduino-cli is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2017-2018 ARDUINO AG (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package sketch |
| 31 | + |
| 32 | +import ( |
| 33 | + "io/ioutil" |
| 34 | + "os" |
| 35 | + "path/filepath" |
| 36 | + |
| 37 | + "github.com/bcmi-labs/arduino-cli/commands" |
| 38 | + "github.com/bcmi-labs/arduino-cli/common/formatter" |
| 39 | + "github.com/bcmi-labs/arduino-cli/configs" |
| 40 | + "github.com/spf13/cobra" |
| 41 | +) |
| 42 | + |
| 43 | +func init() { |
| 44 | + command.AddCommand(newCommand) |
| 45 | +} |
| 46 | + |
| 47 | +var newCommand = &cobra.Command{ |
| 48 | + Use: "new", |
| 49 | + Short: "Create a new Sketch", |
| 50 | + Long: "Create a new Sketch", |
| 51 | + Example: "arduino sketch new MultiBlinker", |
| 52 | + Args: cobra.ExactArgs(1), |
| 53 | + Run: runNewCommand, |
| 54 | +} |
| 55 | + |
| 56 | +var emptySketch = []byte(` |
| 57 | +void setup() { |
| 58 | +} |
| 59 | +
|
| 60 | +void loop() { |
| 61 | +} |
| 62 | +`) |
| 63 | + |
| 64 | +func runNewCommand(cmd *cobra.Command, args []string) { |
| 65 | + sketchbook, err := configs.ArduinoHomeFolder.Get() |
| 66 | + if err != nil { |
| 67 | + formatter.PrintError(err, "Cannot get sketchbook folder.") |
| 68 | + os.Exit(commands.ErrCoreConfig) |
| 69 | + } |
| 70 | + |
| 71 | + sketchDir := filepath.Join(sketchbook, args[0]) |
| 72 | + if err := os.Mkdir(sketchDir, 0755); err != nil { |
| 73 | + formatter.PrintError(err, "Could not create sketch folder.") |
| 74 | + os.Exit(commands.ErrGeneric) |
| 75 | + } |
| 76 | + |
| 77 | + sketchFile := filepath.Join(sketchDir, args[0]+".ino") |
| 78 | + if err := ioutil.WriteFile(sketchFile, emptySketch, 0644); err != nil { |
| 79 | + formatter.PrintError(err, "Error creating sketch.") |
| 80 | + os.Exit(commands.ErrGeneric) |
| 81 | + } |
| 82 | + |
| 83 | + formatter.Print("Sketch created in: " + sketchDir) |
| 84 | +} |
0 commit comments