github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/login/login.go (about)

     1  // Copyright 2020 Google LLC
     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  //     https://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 login provides an implementation of "gactions login" command.
    16  package login
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/actions-on-google/gactions/api/apiutils"
    22  	"github.com/actions-on-google/gactions/log"
    23  	"github.com/actions-on-google/gactions/project"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  // AddCommand adds the push sub-command to the passed in root command.
    28  func AddCommand(ctx context.Context, root *cobra.Command, proj project.Project) {
    29  	login := &cobra.Command{
    30  		Use:   "login",
    31  		Short: "Authenticate gactions CLI to your Google account via web browser.",
    32  		Long:  "Authenticate gactions CLI to your Google account via web browser.",
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			secret, err := proj.ClientSecretJSON()
    35  			if err != nil {
    36  				return err
    37  			}
    38  			if err := apiutils.Auth(ctx, secret); err != nil {
    39  				return err
    40  			}
    41  			log.DoneMsgln("Successfully logged in.")
    42  			return nil
    43  		},
    44  		Args: cobra.NoArgs,
    45  	}
    46  	root.AddCommand(login)
    47  }