Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
goSDN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danet
goSDN
Commits
a7bdc7c6
Commit
a7bdc7c6
authored
4 years ago
by
Martin Stiemerling
Browse files
Options
Downloads
Patches
Plain Diff
gosdn-cli with working command selection. Stubs mostly for grpc calls.
parent
9fdfa860
No related branches found
No related tags found
1 merge request
!18
Develop
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gosdn-cli/gosdn-cli.go
+120
-5
120 additions, 5 deletions
gosdn-cli/gosdn-cli.go
with
120 additions
and
5 deletions
gosdn-cli/gosdn-cli.go
+
120
−
5
View file @
a7bdc7c6
...
@@ -3,10 +3,13 @@ package main
...
@@ -3,10 +3,13 @@ package main
import
(
import
(
pb
"code.fbi.h-da.de/cocsn/gosdn/cliInterface"
pb
"code.fbi.h-da.de/cocsn/gosdn/cliInterface"
"context"
"context"
"fmt"
"github.com/rivo/tview"
"google.golang.org/grpc"
"google.golang.org/grpc"
"log"
"log"
"os"
"os"
"time"
"time"
"flag"
)
)
const
(
const
(
...
@@ -14,16 +17,110 @@ const (
...
@@ -14,16 +17,110 @@ const (
defaultName
=
"gosdn-cli"
defaultName
=
"gosdn-cli"
)
)
// Based on the helloworld example of grpc.io
// Based on the helloworld example of grpc.io -- thx!
const
refreshInterval
=
500
*
time
.
Millisecond
var
(
view
*
tview
.
Modal
app
*
tview
.
Application
)
type
cliClientConfig
struct
{
goSDNCLIAddr4
*
string
goSDNCLIPort4
*
int
interactive
*
bool
goSDNCommand
*
string
}
type
commandOptions
struct
{
name
string
description
string
command
func
(
conn
*
grpc
.
ClientConn
)
()
}
var
commandList
=
map
[
string
]
commandOptions
{
"hello"
:
{
"hello"
,
"test connection to goSDN controller"
,
goSDNSayHello
},
"shutdown"
:
{
"shutdown"
,
"request goSDN controller to shutdown"
,
goSDNShutdown
},
"testdb"
:
{
"testdb"
,
"test all database connections"
,
goSDNTestDB
},
}
/*
gosdn-cli allows to mode of operations:
- interactive: text GUI to operate goSDN
- non-interactive: basic CLI without text GUI
*/
func
main
()
{
func
main
()
{
// This holds the basic configuration for gosdn-cli
var
myConfiguration
=
new
(
cliClientConfig
)
myConfiguration
.
goSDNCLIAddr4
=
flag
.
String
(
"cliServerAddr"
,
"127.0.0.1"
,
"The IPv4 Address of the grpcCLI."
)
myConfiguration
.
goSDNCLIPort4
=
flag
.
Int
(
"cliServerPort"
,
55055
,
"The port number of the grpcCLI"
)
myConfiguration
.
interactive
=
flag
.
Bool
(
"interactive"
,
false
,
"interactive: text gui or just not"
)
var
printCommandList
=
flag
.
Bool
(
"commandlist"
,
false
,
"interactive: print command list"
)
myConfiguration
.
goSDNCommand
=
flag
.
String
(
"command"
,
""
,
"-command: <your command> ; show commands with -commandlist"
)
flag
.
Parse
()
// Print complete command list and exit
if
(
*
printCommandList
==
true
)
{
for
_
,
element
:=
range
commandList
{
fmt
.
Println
(
element
.
name
+
"
\t
"
+
element
.
description
)
}
os
.
Exit
(
0
)
}
log
.
Println
(
"Starting "
+
defaultName
+
" to access the goSDN controller"
)
// Prepare string with socket for connection to the goSDN controller
goSDNSocketAddress
:=
fmt
.
Sprintf
(
"%s:%d"
,
*
myConfiguration
.
goSDNCLIAddr4
,
*
myConfiguration
.
goSDNCLIPort4
)
log
.
Println
(
"Connecting to the goSDN server at: "
+
goSDNSocketAddress
)
// Set up a connection to the server.
// Set up a connection to the server.
address
:=
"localhost:55055"
address
:=
"localhost:55055"
conn
,
err
:=
grpc
.
Dial
(
address
,
grpc
.
WithInsecure
(),
grpc
.
WithBlock
())
conn
,
err
:=
grpc
.
Dial
(
address
,
grpc
.
WithInsecure
(),
grpc
.
WithBlock
())
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"did not connect: %v"
,
err
)
log
.
Fatalf
(
"did not connect: %v"
,
err
)
}
}
defer
conn
.
Close
()
defer
conn
.
Close
()
log
.
Println
((
"Connected to "
+
conn
.
Target
()))
// Check for non-interactive or interactive mode
if
(
*
myConfiguration
.
interactive
==
false
)
{
log
.
Println
(
"starting in non-interactive mode"
)
// Lookup command or die
_
,
found
:=
commandList
[
*
myConfiguration
.
goSDNCommand
]
if
(
found
)
{
// Excecute desired command
commandList
[
*
myConfiguration
.
goSDNCommand
]
.
command
(
conn
)
}
else
{
log
.
Fatalf
(
"Your desired command %s is not available"
,
commandList
[
*
myConfiguration
.
goSDNCommand
]
.
name
)
os
.
Exit
(
1
)
}
}
else
{
log
.
Println
(
"starting in interactive mode -- do not use yet"
)
os
.
Exit
(
1
)
app
=
tview
.
NewApplication
()
.
EnableMouse
(
true
)
flex
:=
tview
.
NewFlex
()
.
AddItem
(
tview
.
NewBox
()
.
SetBorder
(
true
)
.
SetTitle
(
"Command List"
),
0
,
1
,
false
)
.
AddItem
(
tview
.
NewFlex
()
.
SetDirection
(
tview
.
FlexRow
)
.
AddItem
(
tview
.
NewBox
()
.
SetBorder
(
true
)
.
SetTitle
(
"Top"
),
0
,
1
,
false
)
.
AddItem
(
tview
.
NewBox
()
.
SetBorder
(
true
)
.
SetTitle
(
"Middle (3 x height of Top)"
),
0
,
3
,
false
)
.
AddItem
(
tview
.
NewBox
()
.
SetBorder
(
true
)
.
SetTitle
(
"Bottom (5 rows)"
),
5
,
1
,
false
),
0
,
2
,
false
)
if
err
:=
app
.
SetRoot
(
flex
,
true
)
.
Run
();
err
!=
nil
{
panic
(
err
)
}
}
}
func
goSDNSayHello
(
conn
*
grpc
.
ClientConn
)
{
c
:=
pb
.
NewGreeterClient
(
conn
)
c
:=
pb
.
NewGreeterClient
(
conn
)
// Contact the server and print out its response.
// Contact the server and print out its response.
...
@@ -35,13 +132,31 @@ func main() {
...
@@ -35,13 +132,31 @@ func main() {
defer
cancel
()
defer
cancel
()
r
,
err
:=
c
.
SayHello
(
ctx
,
&
pb
.
HelloRequest
{
Name
:
name
})
r
,
err
:=
c
.
SayHello
(
ctx
,
&
pb
.
HelloRequest
{
Name
:
name
})
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"could not
greet
: %v"
,
err
)
log
.
Fatalf
(
"could not
say hello
: %v"
,
err
)
}
}
log
.
Printf
(
"Greeting: %s"
,
r
.
GetMessage
())
log
.
Printf
(
"Greeting: %s"
,
r
.
GetMessage
())
r2
,
err
:=
c
.
Shutdown
(
ctx
,
&
pb
.
ShutdownRequest
{
Name
:
name
})
}
func
goSDNShutdown
(
conn
*
grpc
.
ClientConn
)
{
c
:=
pb
.
NewGreeterClient
(
conn
)
// Contact the server and print out its response.
name
:=
defaultName
if
len
(
os
.
Args
)
>
1
{
name
=
os
.
Args
[
1
]
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
)
defer
cancel
()
r
,
err
:=
c
.
Shutdown
(
ctx
,
&
pb
.
ShutdownRequest
{
Name
:
name
})
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"could not
g
re
et
: %v"
,
err
)
log
.
Fatalf
(
"could not re
quest shutdown
: %v"
,
err
)
}
}
log
.
Printf
(
"Greeting: %s"
,
r
2
.
GetMessage
())
log
.
Printf
(
"Greeting: %s"
,
r
.
GetMessage
())
}
}
func
goSDNTestDB
(
conn
*
grpc
.
ClientConn
){
// TODO: fill with code
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment