|
| 1 | +// Copyright © 2018 Jonathan Pentecost <[email protected]> |
| 2 | +// |
| 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 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strconv" |
| 20 | + |
| 21 | + "github.com/spf13/cobra" |
| 22 | +) |
| 23 | + |
| 24 | +// volumeCmd represents the volume command |
| 25 | +var volumeCmd = &cobra.Command{ |
| 26 | + Use: "volume [<0.00 - 1.00>]", |
| 27 | + Short: "Get or set volume", |
| 28 | + Long: "Get or set volume (float in range from 0 to 1)", |
| 29 | + Run: func(cmd *cobra.Command, args []string) { |
| 30 | + app, err := castApplication(cmd, args) |
| 31 | + if err != nil { |
| 32 | + fmt.Printf("unable to get cast application: %v\n", err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if len(args) == 1 && args[0] != "" { |
| 37 | + newVolume, err := strconv.ParseFloat(args[0], 32) |
| 38 | + if err != nil { |
| 39 | + fmt.Printf("invalid volume: %v\n", err) |
| 40 | + return |
| 41 | + } |
| 42 | + if err = app.SetVolume(float32(newVolume)); err != nil { |
| 43 | + fmt.Printf("failed to set volume: %v\n", err) |
| 44 | + return |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if err = app.Update(); err != nil { |
| 49 | + fmt.Printf("unable to update cast info: %v\n", err) |
| 50 | + return |
| 51 | + } |
| 52 | + _, _, castVolume := app.Status() |
| 53 | + |
| 54 | + fmt.Printf("%0.2f\n", castVolume.Level) |
| 55 | + |
| 56 | + return |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +func init() { |
| 61 | + rootCmd.AddCommand(volumeCmd) |
| 62 | +} |
0 commit comments