Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 41 additions & 55 deletions cmd/src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var configCommands commander
func init() {
usage := `'src config' is a tool that manages global, organization, and user settings on a Sourcegraph instance.

The effective configuration is computed by shallow-merging the following settings, in order from lowest to highest precedence:
The effective setting is computed by shallow-merging the following settings, in order from lowest to highest precedence:

- Global settings (site-wide)
- Organization settings for the user's organizations (if any)
Expand All @@ -26,9 +26,9 @@ Usage:

The commands are:

get gets the effective (merged) configuration
edit updates configuration settings
list lists the partial settings (that, when merged, yield the effective configuration)
get gets the effective (merged) settings
edit updates settings
list lists the partial settings (that, when merged, yield the effective settings)

Use "src config [command] -h" for more information about a command.
`
Expand All @@ -49,14 +49,12 @@ Use "src config [command] -h" for more information about a command.
})
}

const configurationSubjectFragment = `
fragment ConfigurationSubjectFields on ConfigurationSubject {
const settingsSubjectFragment = `
fragment SettingsSubjectFields on SettingsSubject {
id
latestSettings {
id
configuration {
...ConfigurationFields
}
contents
author {
...UserFields
}
Expand All @@ -67,60 +65,48 @@ fragment ConfigurationSubjectFields on ConfigurationSubject {
}
`

type ConfigurationSubject struct {
ID string
LatestSettings *Settings
SettingsURL string
ViewerCanAdminister bool
ConfigurationCascade ConfigurationCascade
type SettingsSubject struct {
ID string
LatestSettings *Settings
SettingsURL string
ViewerCanAdminister bool
SettingsCascade SettingsCascade
}

type Settings struct {
ID int32
Configuration Configuration
Author *User
CreatedAt string
ID int32
Contents string
Author *User
CreatedAt string
}

const configurationCascadeFragment = `
fragment ConfigurationCascadeFields on ConfigurationCascade {
const settingsCascadeFragment = `
fragment SettingsCascadeFields on SettingsCascade {
subjects {
...ConfigurationSubjectFields
}
merged {
...ConfigurationFields
...SettingsSubjectFields
}
final
}
`

type ConfigurationCascade struct {
Subjects []ConfigurationSubject
Merged Configuration
}

const configurationFragment = `
fragment ConfigurationFields on Configuration {
contents
}
`

type Configuration struct {
Contents string
type SettingsCascade struct {
Subjects []SettingsSubject
Final string
}

const viewerConfigurationQuery = `query ViewerConfiguration {
viewerConfiguration {
...ConfigurationCascadeFields
const viewerSettingsQuery = `query ViewerSettings {
viewerSettings {
...SettingsCascadeFields
}
}` + configurationCascadeFragment + configurationSubjectFragment + configurationFragment + userFragment
}` + settingsCascadeFragment + settingsSubjectFragment + userFragment

const configurationSubjectCascadeQuery = `query ConfigurationSubjectCascade($subject: ID!) {
configurationSubject(id: $subject) {
configurationCascade {
...ConfigurationCascadeFields
const settingsSubjectCascadeQuery = `query SettingsSubjectCascade($subject: ID!) {
settingsSubject(id: $subject) {
settingsCascade {
...SettingsCascadeFields
}
}
}` + configurationCascadeFragment + configurationSubjectFragment + configurationFragment + userFragment
}` + settingsCascadeFragment + settingsSubjectFragment + userFragment

type KeyPath struct {
Property string `json:"property,omitempty"`
Expand Down Expand Up @@ -150,18 +136,18 @@ query ViewerUserID {
return result.CurrentUser.ID, nil
}

func getConfigurationSubjectLatestSettingsID(subjectID string) (*int, error) {
func getSettingsSubjectLatestSettingsID(subjectID string) (*int, error) {
var result struct {
ConfigurationSubject *struct {
SettingsSubject *struct {
LatestSettings *struct {
ID int
}
}
}
req := &apiRequest{
query: `
query ConfigurationSubjectLatestSettingsID($subject: ID!) {
configurationSubject(id: $subject) {
query SettingsSubjectLatestSettingsID($subject: ID!) {
settingsSubject(id: $subject) {
latestSettings {
id
}
Expand All @@ -174,11 +160,11 @@ query ConfigurationSubjectLatestSettingsID($subject: ID!) {
if err := req.do(); err != nil {
return nil, err
}
if result.ConfigurationSubject == nil {
return nil, fmt.Errorf("unable to find configuration subject with ID %s", subjectID)
if result.SettingsSubject == nil {
return nil, fmt.Errorf("unable to find settings subject with ID %s", subjectID)
}
if result.ConfigurationSubject.LatestSettings == nil {
if result.SettingsSubject.LatestSettings == nil {
return nil, nil
}
return &result.ConfigurationSubject.LatestSettings.ID, nil
return &result.SettingsSubject.LatestSettings.ID, nil
}
28 changes: 14 additions & 14 deletions cmd/src/config_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ func init() {
usage := `
Examples:

Edit configuration property for the current user (authenticated by the src CLI's access token, if any):
Edit settings property for the current user (authenticated by the src CLI's access token, if any):

$ src config edit -property motd -value '["Hello!"]'

Overwrite all configuration settings for the current user:
Overwrite all settings settings for the current user:

$ src config edit -overwrite -value '{"motd":["Hello!"]}'

Overwrite all configuration settings for the current user with the file contents:
Overwrite all settings settings for the current user with the file contents:

$ src config edit -overwrite -value-file myconfig.json

Edit a configuration property for the user with username alice:
Edit a settings property for the user with username alice:

$ src config edit -subject=$(src users get -f '{{.ID}}' -username=alice) -property motd -value '["Hello!"]'

Overwrite all configuration settings for the organization named abc-org:
Overwrite all settings settings for the organization named abc-org:

$ src config edit -subject=$(src orgs get -f '{{.ID}}' -name=abc-org) -overwrite -value '{"motd":["Hello!"]}'

Expand All @@ -40,9 +40,9 @@ Examples:
fmt.Println(usage)
}
var (
subjectFlag = flagSet.String("subject", "", "The ID of the configuration subject whose configuration to edit. (default: authenticated user)")
propertyFlag = flagSet.String("property", "", "The name of the configuration property to set.")
valueFlag = flagSet.String("value", "", "The value for the configuration property (when used with -property).")
subjectFlag = flagSet.String("subject", "", "The ID of the settings subject whose settings to edit. (default: authenticated user)")
propertyFlag = flagSet.String("property", "", "The name of the settings property to set.")
valueFlag = flagSet.String("value", "", "The value for the settings property (when used with -property).")
valueFileFlag = flagSet.String("value-file", "", "Read the value from this file instead of from the -value command-line option.")
overwriteFlag = flagSet.Bool("overwrite", false, "Overwrite the entire settings with the value given in -value (not just a single property).")
apiFlags = newAPIFlags(flagSet)
Expand Down Expand Up @@ -85,15 +85,15 @@ Examples:
subjectID = *subjectFlag
}

lastID, err := getConfigurationSubjectLatestSettingsID(subjectID)
lastID, err := getSettingsSubjectLatestSettingsID(subjectID)
if err != nil {
return err
}

query := `
mutation EditConfiguration($input: ConfigurationMutationGroupInput!, $edit: ConfigurationEdit!) {
configurationMutation(input: $input) {
editConfiguration(edit: $edit) {
mutation EditSettings($input: SettingsMutationGroupInput!, $edit: SettingsEdit!) {
settingsMutation(input: $input) {
editSettings(edit: $edit) {
empty {
alwaysNil
}
Expand All @@ -113,8 +113,8 @@ mutation EditConfiguration($input: ConfigurationMutationGroupInput!, $edit: Conf
}

var result struct {
ViewerConfiguration *ConfigurationCascade
ConfigurationSubject *ConfigurationSubject
ViewerSettings *SettingsCascade
SettingsSubject *SettingsSubject
}
return (&apiRequest{
query: query,
Expand Down
30 changes: 15 additions & 15 deletions cmd/src/config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ func init() {
usage := `
Examples:

Get configuration for the current user (authenticated by the src CLI's access token, if any):
Get settings for the current user (authenticated by the src CLI's access token, if any):

$ src config get

Get configuration for the user with username alice:
Get settings for the user with username alice:

$ src config get -subject=$(src users get -f '{{.ID}}' -username=alice)

Get configuration for the organization named abc-org:
Get settings for the organization named abc-org:

$ src config get -subject=$(src orgs get -f '{{.ID}}' -name=abc-org)

Expand All @@ -30,8 +30,8 @@ Examples:
fmt.Println(usage)
}
var (
subjectFlag = flagSet.String("subject", "", "The ID of the configuration subject whose configuration to get. (default: authenticated user)")
formatFlag = flagSet.String("f", "{{.Contents|jsonIndent}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.|json}}")`)
subjectFlag = flagSet.String("subject", "", "The ID of the settings subject whose settings to get. (default: authenticated user)")
formatFlag = flagSet.String("f", "{{.|jsonIndent}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.|json}}")`)
apiFlags = newAPIFlags(flagSet)
)

Expand All @@ -49,30 +49,30 @@ Examples:
var query string
var queryVars map[string]interface{}
if *subjectFlag == "" {
query = viewerConfigurationQuery
query = viewerSettingsQuery
} else {
query = configurationSubjectCascadeQuery
query = settingsSubjectCascadeQuery
queryVars = map[string]interface{}{
"subject": nullString(*subjectFlag),
}
}

var result struct {
ViewerConfiguration *ConfigurationCascade
ConfigurationSubject *ConfigurationSubject
ViewerSettings *SettingsCascade
SettingsSubject *SettingsSubject
}
return (&apiRequest{
query: query,
vars: queryVars,
result: &result,
done: func() error {
var merged Configuration
if result.ViewerConfiguration != nil {
merged = result.ViewerConfiguration.Merged
} else if result.ConfigurationSubject != nil {
merged = result.ConfigurationSubject.ConfigurationCascade.Merged
var final string
if result.ViewerSettings != nil {
final = result.ViewerSettings.Final
} else if result.SettingsSubject != nil {
final = result.SettingsSubject.SettingsCascade.Final
}
return execTemplate(tmpl, merged)
return execTemplate(tmpl, final)
},
flags: apiFlags,
}).do()
Expand Down
26 changes: 13 additions & 13 deletions cmd/src/config_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ func init() {
usage := `
Examples:

List configuration settings for the current user (authenticated by the src CLI's access token, if any):
List settings for the current user (authenticated by the src CLI's access token, if any):

$ src config list

List configuration settings for the user with username alice:
List settings for the user with username alice:

$ src config list -subject=$(src users get -f '{{.ID}}' -username=alice)

Expand All @@ -26,7 +26,7 @@ Examples:
fmt.Println(usage)
}
var (
subjectFlag = flagSet.String("subject", "", "The ID of the configuration subject whose configuration to list. (default: authenticated user)")
subjectFlag = flagSet.String("subject", "", "The ID of the settings subject whose settings to list. (default: authenticated user)")
formatFlag = flagSet.String("f", "", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.|json}}")`)
apiFlags = newAPIFlags(flagSet)
)
Expand All @@ -44,7 +44,7 @@ Examples:
// Set default here instead of in flagSet.String because it is very long and makes the usage message ugly.
formatStr = `{{range .Subjects -}}
# {{.SettingsURL}}:{{with .LatestSettings}}
{{.Configuration.Contents}}
{{.Contents}}
{{- else}} (empty){{- end}}
{{end}}`
}
Expand All @@ -56,28 +56,28 @@ Examples:
var query string
var queryVars map[string]interface{}
if *subjectFlag == "" {
query = viewerConfigurationQuery
query = viewerSettingsQuery
} else {
query = configurationSubjectCascadeQuery
query = settingsSubjectCascadeQuery
queryVars = map[string]interface{}{
"subject": nullString(*subjectFlag),
}
}

var result struct {
ViewerConfiguration *ConfigurationCascade
ConfigurationSubject *ConfigurationSubject
ViewerSettings *SettingsCascade
SettingsSubject *SettingsSubject
}
return (&apiRequest{
query: query,
vars: queryVars,
result: &result,
done: func() error {
var cascade *ConfigurationCascade
if result.ViewerConfiguration != nil {
cascade = result.ViewerConfiguration
} else if result.ConfigurationSubject != nil {
cascade = &result.ConfigurationSubject.ConfigurationCascade
var cascade *SettingsCascade
if result.ViewerSettings != nil {
cascade = result.ViewerSettings
} else if result.SettingsSubject != nil {
cascade = &result.SettingsSubject.SettingsCascade
}
return execTemplate(tmpl, cascade)
},
Expand Down