-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add a new command to refresh the access token (#1668)
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
- Loading branch information
Showing
6 changed files
with
122 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2023 The Perses Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package refresh | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
persesCMD "github.com/perses/perses/internal/cli/cmd" | ||
"github.com/perses/perses/internal/cli/config" | ||
"github.com/perses/perses/internal/cli/output" | ||
"github.com/perses/perses/pkg/client/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type option struct { | ||
persesCMD.Option | ||
writer io.Writer | ||
apiClient api.ClientInterface | ||
} | ||
|
||
func (o *option) Complete(args []string) error { | ||
if len(args) > 0 { | ||
return fmt.Errorf("no args are supported by the command 'apply'") | ||
} | ||
apiClient, err := config.Global.GetAPIClient() | ||
if err != nil { | ||
return err | ||
} | ||
o.apiClient = apiClient | ||
return nil | ||
} | ||
|
||
func (o *option) Validate() error { | ||
if len(config.Global.RefreshToken) == 0 { | ||
return fmt.Errorf("refresh_token doesn't exist in the config, please use the command login to get one") | ||
} | ||
return nil | ||
} | ||
|
||
func (o *option) Execute() error { | ||
response, err := o.apiClient.Auth().Refresh(config.Global.RefreshToken) | ||
if err != nil { | ||
return err | ||
} | ||
if writeErr := config.SetAccessToken(response.AccessToken); writeErr != nil { | ||
return writeErr | ||
} | ||
return output.HandleString(o.writer, "access token has been refreshed") | ||
} | ||
|
||
func (o *option) SetWriter(writer io.Writer) { | ||
o.writer = writer | ||
} | ||
|
||
func NewCMD() *cobra.Command { | ||
o := &option{} | ||
cmd := &cobra.Command{ | ||
Use: "refresh", | ||
Short: "refresh the access token when it expires", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return persesCMD.Run(o, cmd, args) | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters