-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
551 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// Copyright 2020-2023 Buf Technologies, Inc. | ||
// | ||
// 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 graph | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/bufbuild/buf/private/buf/bufcli" | ||
"github.com/bufbuild/buf/private/buf/buffetch" | ||
"github.com/bufbuild/buf/private/buf/bufwire" | ||
"github.com/bufbuild/buf/private/bufpkg/bufanalysis" | ||
"github.com/bufbuild/buf/private/bufpkg/bufapimodule" | ||
"github.com/bufbuild/buf/private/bufpkg/bufgraph" | ||
"github.com/bufbuild/buf/private/bufpkg/bufmodule" | ||
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild" | ||
"github.com/bufbuild/buf/private/pkg/app/appcmd" | ||
"github.com/bufbuild/buf/private/pkg/app/appflag" | ||
"github.com/bufbuild/buf/private/pkg/command" | ||
"github.com/bufbuild/buf/private/pkg/stringutil" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
const ( | ||
errorFormatFlagName = "error-format" | ||
configFlagName = "config" | ||
disableSymlinksFlagName = "disable-symlinks" | ||
) | ||
|
||
// NewCommand returns a new Command. | ||
func NewCommand( | ||
name string, | ||
builder appflag.Builder, | ||
) *appcmd.Command { | ||
flags := newFlags() | ||
return &appcmd.Command{ | ||
Use: name + " <input>", | ||
Short: "Print the dependency graph in DOT format", | ||
Long: bufcli.GetSourceOrModuleLong(`the source or module to print for`), | ||
Args: cobra.MaximumNArgs(1), | ||
Run: builder.NewRunFunc( | ||
func(ctx context.Context, container appflag.Container) error { | ||
return run(ctx, container, flags) | ||
}, | ||
bufcli.NewErrorInterceptor(), | ||
), | ||
BindFlags: flags.Bind, | ||
} | ||
} | ||
|
||
type flags struct { | ||
ErrorFormat string | ||
Config string | ||
DisableSymlinks bool | ||
// special | ||
InputHashtag string | ||
} | ||
|
||
func newFlags() *flags { | ||
return &flags{} | ||
} | ||
|
||
func (f *flags) Bind(flagSet *pflag.FlagSet) { | ||
bufcli.BindInputHashtag(flagSet, &f.InputHashtag) | ||
bufcli.BindDisableSymlinks(flagSet, &f.DisableSymlinks, disableSymlinksFlagName) | ||
flagSet.StringVar( | ||
&f.ErrorFormat, | ||
errorFormatFlagName, | ||
"text", | ||
fmt.Sprintf( | ||
"The format for build errors printed to stderr. Must be one of %s", | ||
stringutil.SliceToString(bufanalysis.AllFormatStrings), | ||
), | ||
) | ||
flagSet.StringVar( | ||
&f.Config, | ||
configFlagName, | ||
"", | ||
`The file or data to use to use for configuration`, | ||
) | ||
} | ||
|
||
func run( | ||
ctx context.Context, | ||
container appflag.Container, | ||
flags *flags, | ||
) error { | ||
if err := bufcli.ValidateErrorFormatFlag(flags.ErrorFormat, errorFormatFlagName); err != nil { | ||
return err | ||
} | ||
input, err := bufcli.GetInputValue(container, flags.InputHashtag, ".") | ||
if err != nil { | ||
return err | ||
} | ||
sourceOrModuleRef, err := buffetch.NewRefParser(container.Logger()).GetSourceOrModuleRef(ctx, input) | ||
if err != nil { | ||
return err | ||
} | ||
storageosProvider := bufcli.NewStorageosProvider(flags.DisableSymlinks) | ||
runner := command.NewRunner() | ||
clientConfig, err := bufcli.NewConnectClientConfig(container) | ||
if err != nil { | ||
return err | ||
} | ||
moduleResolver := bufapimodule.NewModuleResolver( | ||
container.Logger(), | ||
bufapimodule.NewRepositoryCommitServiceClientFactory(clientConfig), | ||
) | ||
moduleReader, err := bufcli.NewModuleReaderAndCreateCacheDirs(container, clientConfig) | ||
if err != nil { | ||
return err | ||
} | ||
moduleConfigReader := bufwire.NewModuleConfigReader( | ||
container.Logger(), | ||
storageosProvider, | ||
bufcli.NewFetchReader(container.Logger(), storageosProvider, runner, moduleResolver, moduleReader), | ||
bufmodulebuild.NewModuleBucketBuilder(), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
graphBuilder := bufgraph.NewBuilder( | ||
container.Logger(), | ||
moduleResolver, | ||
moduleReader, | ||
) | ||
moduleConfigSet, err := moduleConfigReader.GetModuleConfigSet( | ||
ctx, | ||
container, | ||
sourceOrModuleRef, | ||
flags.Config, | ||
nil, | ||
nil, | ||
false, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
moduleConfigs := moduleConfigSet.ModuleConfigs() | ||
modules := make([]bufmodule.Module, len(moduleConfigs)) | ||
for i, moduleConfig := range moduleConfigs { | ||
modules[i] = moduleConfig.Module() | ||
} | ||
graph, fileAnnotations, err := graphBuilder.Build( | ||
ctx, | ||
modules, | ||
bufgraph.BuildWithWorkspace(moduleConfigSet.Workspace()), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if len(fileAnnotations) > 0 { | ||
// stderr since we do output to stdout potentially | ||
if err := bufanalysis.PrintFileAnnotations( | ||
container.Stderr(), | ||
fileAnnotations, | ||
flags.ErrorFormat, | ||
); err != nil { | ||
return err | ||
} | ||
return bufcli.ErrFileAnnotation | ||
} | ||
dotString, err := graph.DOTString( | ||
func(node bufgraph.Node) string { | ||
return node.String() | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprintln(container.Stdout(), dotString) | ||
return err | ||
} |
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,19 @@ | ||
// Copyright 2020-2023 Buf Technologies, Inc. | ||
// | ||
// 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. | ||
|
||
// Generated. DO NOT EDIT. | ||
|
||
package graph | ||
|
||
import _ "github.com/bufbuild/buf/private/usage" |
Oops, something went wrong.