Skip to content

Commit

Permalink
refactor: add root package for Cobra commands (#825)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Mar 30, 2023
1 parent 67dca8e commit 07a754e
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 38 deletions.
30 changes: 6 additions & 24 deletions cmd/oras/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,17 @@ limitations under the License.
package main

import (
"context"
"os"
"os/signal"

"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/blob"
"oras.land/oras/cmd/oras/manifest"
repository "oras.land/oras/cmd/oras/repo"
"oras.land/oras/cmd/oras/tag"
"oras.land/oras/cmd/oras/root"
)

func main() {
cmd := &cobra.Command{
Use: "oras [command]",
SilenceUsage: true,
}
cmd.AddCommand(
pullCmd(),
pushCmd(),
loginCmd(),
logoutCmd(),
versionCmd(),
discoverCmd(),
copyCmd(),
attachCmd(),
blob.Cmd(),
manifest.Cmd(),
tag.TagCmd(),
repository.Cmd(),
)
if err := cmd.Execute(); err != nil {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := root.New().ExecuteContext(ctx); err != nil {
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion cmd/oras/attach.go → cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"context"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions cmd/oras/root/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright The ORAS 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 root

import (
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/root/blob"
"oras.land/oras/cmd/oras/root/manifest"
"oras.land/oras/cmd/oras/root/repo"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "oras [command]",
SilenceUsage: true,
}
cmd.AddCommand(
pullCmd(),
pushCmd(),
loginCmd(),
logoutCmd(),
versionCmd(),
discoverCmd(),
copyCmd(),
tagCmd(),
attachCmd(),
blob.Cmd(),
manifest.Cmd(),
repo.Cmd(),
)
return cmd
}
2 changes: 1 addition & 1 deletion cmd/oras/cp.go → cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/discover.go → cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/file.go → cmd/oras/root/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/login.go → cmd/oras/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/logout.go → cmd/oras/root/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"github.com/sirupsen/logrus"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/oras/pull.go → cmd/oras/root/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/push.go → cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/repo/cmd.go → cmd/oras/root/repo/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package repository
package repo

import (
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/repo/ls.go → cmd/oras/root/repo/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package repository
package repo

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/repo/tags.go → cmd/oras/root/repo/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package repository
package repo

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/tag/tag.go → cmd/oras/root/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package tag
package root

import (
"fmt"
Expand All @@ -33,7 +33,7 @@ type tagOptions struct {
targetRefs []string
}

func TagCmd() *cobra.Command {
func tagCmd() *cobra.Command {
var opts tagOptions
cmd := &cobra.Command{
Use: "tag [flags] <name>{:<tag>|@<digest>} <new_tag> [...]",
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/version.go → cmd/oras/root/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package root

import (
"fmt"
Expand Down
1 change: 1 addition & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 repository

import (
Expand Down

0 comments on commit 07a754e

Please sign in to comment.