-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strm-1198): implement context switching for projects and add pro…
…jectId to requests
- Loading branch information
Showing
33 changed files
with
482 additions
and
215 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
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
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,91 @@ | ||
package context | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strmprivacy/strm/pkg/common" | ||
"strmprivacy/strm/pkg/entity/project" | ||
) | ||
|
||
const activeProjectFilename = "active_project" | ||
|
||
// ResolveProject resolves the project to use and makes its ID globally available. | ||
// The value passed through the flag takes precedence, then the value stored in the config dir, and finally | ||
// a fallback to default project. | ||
func ResolveProject(f *pflag.FlagSet) { | ||
|
||
activeProjectFilePath := path.Join(common.ConfigPath, activeProjectFilename) | ||
projectFlagValue, _ := f.GetString(ProjectFlag) | ||
|
||
if _, err := os.Stat(activeProjectFilePath); (os.IsNotExist(err) || GetActiveProject() == "") && projectFlagValue == "" { | ||
initActiveProject() | ||
fmt.Println(fmt.Sprintf("Active project was not yet set, has been set to '%v'. You can set a project "+ | ||
"with 'strm context project <project-name>'\n", GetActiveProject())) | ||
} | ||
|
||
if projectFlagValue != "" { | ||
resolvedProject := project.GetProject(projectFlagValue) | ||
if resolvedProject == nil { | ||
message := fmt.Sprintf("Specified project '%v' does not exist, or you do not have access to it.", projectFlagValue) | ||
common.CliExit(errors.New(message)) | ||
} | ||
common.ProjectId = resolvedProject.Id | ||
} else { | ||
activeProject := GetActiveProject() | ||
resolvedProject := project.GetProject(activeProject) | ||
if resolvedProject == nil { | ||
initActiveProject() | ||
common.CliExit(errors.New(fmt.Sprintf("Active project '%v' does not exist, or you do not have access " + | ||
"to it. The following project has been set instead: %v", activeProject, GetActiveProject()))) | ||
} | ||
common.ProjectId = resolvedProject.Id | ||
} | ||
} | ||
|
||
func SetActiveProject(projectName string) { | ||
if project.GetProject(projectName) != nil { | ||
saveActiveProject(projectName) | ||
message := "Active project set to: " + projectName | ||
log.Infoln(message) | ||
fmt.Println(message) | ||
} else { | ||
message := fmt.Sprintf("No project '%v' found, or you do not have access to it.", projectName) | ||
log.Warnln(message) | ||
common.CliExit(errors.New(message)) | ||
} | ||
} | ||
|
||
func GetActiveProject() string { | ||
activeProjectFilePath := path.Join(common.ConfigPath, activeProjectFilename) | ||
|
||
bytes, err := ioutil.ReadFile(activeProjectFilePath) | ||
common.CliExit(err) | ||
activeProject := string(bytes) | ||
log.Infoln("Current active project is: " + activeProject) | ||
return activeProject | ||
} | ||
|
||
func initActiveProject() { | ||
projects := project.ListProjects() | ||
if len(projects.Projects) == 0 { | ||
common.CliExit(errors.New("you do not have access to any projects; create a project first, or ask to be granted access to one")) | ||
} | ||
firstProjectName := projects.Projects[0].Name | ||
saveActiveProject(firstProjectName) | ||
} | ||
|
||
func saveActiveProject(projectName string) { | ||
activeProjectFilepath := path.Join(common.ConfigPath, activeProjectFilename) | ||
|
||
err := ioutil.WriteFile( | ||
activeProjectFilepath, | ||
[]byte(projectName), | ||
0644, | ||
) | ||
common.CliExit(err) | ||
} |
Oops, something went wrong.